use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testIncDec.
@Test
public void testIncDec() {
final Rectangle[] array = { new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(1, 10), new Rectangle(10, 10), new Rectangle(10, 10) };
for (Rectangle rect : array) {
getDs().save(rect);
}
final Query<Rectangle> heightOf1 = getDs().find(Rectangle.class).filter(eq("height", 1D));
final Query<Rectangle> heightOf2 = getDs().find(Rectangle.class).filter(eq("height", 2D));
final Query<Rectangle> heightOf35 = getDs().find(Rectangle.class).filter(eq("height", 3.5D));
assertThat(heightOf1.count(), is(3L));
assertThat(heightOf2.count(), is(0L));
UpdateResult results = heightOf1.update(inc("height")).execute(new UpdateOptions().multi(true));
assertUpdated(results, 3);
assertThat(heightOf1.count(), is(0L));
assertThat(heightOf2.count(), is(3L));
heightOf2.update(dec("height")).execute(new UpdateOptions().multi(true));
assertThat(heightOf1.count(), is(3L));
assertThat(heightOf2.count(), is(0L));
heightOf1.update(inc("height", 2.5D)).execute(new UpdateOptions().multi(true));
assertThat(heightOf1.count(), is(0L));
assertThat(heightOf35.count(), is(3L));
heightOf35.update(dec("height", 2.5D)).execute(new UpdateOptions().multi(true));
assertThat(heightOf1.count(), is(3L));
assertThat(heightOf35.count(), is(0L));
getDs().find(Rectangle.class).filter(eq("height", 1D)).update(set("height", 1D), inc("width", 20D)).execute();
MatcherAssert.assertThat(getDs().find(Rectangle.class).count(), is(5L));
MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("height", 1D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 30D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
getDs().find(Rectangle.class).filter(eq("width", 30D)).update(set("height", 2D), set("width", 2D)).execute();
MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 1D)).iterator(new FindOptions().limit(1)).tryNext(), is(nullValue()));
MatcherAssert.assertThat(getDs().find(Rectangle.class).filter(eq("width", 2D)).iterator(new FindOptions().limit(1)).next(), is(notNullValue()));
heightOf35.update(dec("height", 1)).execute();
heightOf35.update(dec("height", Long.MAX_VALUE)).execute();
heightOf35.update(dec("height", 1.5f)).execute();
heightOf35.update(dec("height", Double.MAX_VALUE)).execute();
try {
heightOf35.update(dec("height", new AtomicInteger(1)));
Assert.fail("Wrong data type not recognized.");
} catch (IllegalArgumentException ignore) {
}
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method shouldUpdateAnArrayElement.
@Test
public void shouldUpdateAnArrayElement() {
// given
ObjectId parentId = new ObjectId();
String childName = "Bob";
String updatedLastName = "updatedLastName";
Parent parent = new Parent();
parent.id = parentId;
parent.children.add(new Child("Anthony", "Child"));
parent.children.add(new Child(childName, "originalLastName"));
getDs().save(parent);
// when
Query<Parent> query = getDs().find(Parent.class).filter(eq("_id", parentId), eq("children.first", childName));
UpdateResult updateResult = query.update(set("children.$.last", updatedLastName)).execute();
// then
assertThat(updateResult.getModifiedCount(), is(1L));
assertThat(getDs().find(Parent.class).filter(eq("id", parentId)).iterator(new FindOptions().limit(1)).next().children, hasItem(new Child(childName, updatedLastName)));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testUpdateKeyList.
@Test
public void testUpdateKeyList() {
final ContainsPicKey cpk = new ContainsPicKey();
cpk.name = "cpk one";
Datastore ds = getDs();
ds.save(cpk);
final Pic pic = new Pic();
pic.setName("fist again");
ds.save(pic);
cpk.keys = MorphiaReference.wrap(List.of(pic));
// test with Key<Pic>
Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
final UpdateResult res = query.update(set("keys", cpk.keys)).execute();
assertThat(res.getModifiedCount(), is(1L));
// test reading the object.
final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
assertThat(cpk2, is(notNullValue()));
assertThat(cpk.name, is(cpk2.name));
MatcherAssert.assertThat(cpk2.keys.get(), Matchers.hasItem(pic));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testExistingUpdates.
@Test
public void testExistingUpdates() {
getDs().save(new Circle(100D));
getDs().save(new Circle(12D));
Query<Circle> circle = getDs().find(Circle.class);
assertUpdated(circle.update(inc("radius", 1D)).execute(), 1);
assertUpdated(circle.update(inc("radius")).execute(new UpdateOptions().multi(true)), 2);
// test possible data type change.
final Circle updatedCircle = circle.filter(eq("radius", 13)).iterator(new FindOptions().limit(1)).next();
assertThat(updatedCircle, is(notNullValue()));
MatcherAssert.assertThat(updatedCircle.getRadius(), is(13D));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testUpdateKeyRef.
@Test
public void testUpdateKeyRef() {
final ContainsPicKey cpk = new ContainsPicKey();
cpk.name = "cpk one";
Datastore ds = getDs();
ds.save(cpk);
final Pic pic = new Pic();
pic.setName("fist again");
ds.save(pic);
Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
assertThat(query.update(set("pic", pic)).execute().getModifiedCount(), is(1L));
// test reading the object.
final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
assertThat(cpk2, is(notNullValue()));
assertThat(cpk.name, is(cpk2.name));
assertThat(cpk2.pic, is(notNullValue()));
MatcherAssert.assertThat(pic, CoreMatchers.is(cpk2.pic.get()));
query.update(set("pic", pic)).execute();
// test reading the object.
final ContainsPicKey cpk3 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
assertThat(cpk3, is(notNullValue()));
assertThat(cpk.name, is(cpk3.name));
assertThat(cpk3.pic, is(notNullValue()));
MatcherAssert.assertThat(pic, CoreMatchers.is(cpk3.pic.get()));
}
Aggregations