use of dev.morphia.UpdateOptions 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.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method doUpdates.
@SuppressWarnings("rawtypes")
private void doUpdates(ContainsIntArray updated, ContainsIntArray control, Update update, Integer[] target) {
assertUpdated(update.execute(new UpdateOptions()), 1);
assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", updated.id)).first().values, is(target));
assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", control.id)).first().values, is(new Integer[] { 1, 2, 3 }));
Assert.assertEquals(update.execute(new UpdateOptions()).getMatchedCount(), 1);
assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", updated.id)).first().values, is(target));
assertThat(getDs().find(ContainsIntArray.class).filter(eq("_id", control.id)).first().values, is(new Integer[] { 1, 2, 3 }));
}
use of dev.morphia.UpdateOptions 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.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testUpsert.
@Test
public void testUpsert() {
ContainsIntArray cIntArray = new ContainsIntArray();
ContainsIntArray control = new ContainsIntArray();
getDs().save(asList(cIntArray, control));
Query<ContainsIntArray> query = getDs().find(ContainsIntArray.class);
doUpdates(cIntArray, control, query.update(addToSet("values", 4)), new Integer[] { 1, 2, 3, 4 });
doUpdates(cIntArray, control, query.update(addToSet("values", asList(4, 5))), new Integer[] { 1, 2, 3, 4, 5 });
assertInserted(getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7 })).update(addToSet("values", 6)).execute(new UpdateOptions().upsert(true)));
query = getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7, 6 }));
FindOptions options = new FindOptions().logQuery();
assertNotNull(query.first(options), query.getLoggedQuery());
}
use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testMinWithDates.
@Test
public void testMinWithDates() {
List<User> entities = List.of(new User("User 1", LocalDate.of(2003, 7, 13)), new User("User 2", LocalDate.of(2009, 12, 1)), new User("User 3", LocalDate.of(2015, 8, 19)));
getDs().save(entities);
UpdateResult updated = getDs().find(User.class).update(min("joined", LocalDate.of(1985, 10, 12))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
getDs().find(User.class).delete();
getDs().save(entities);
updated = getDs().find(User.class).update(min("joined", Instant.now().minus(5000, DAYS))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 2);
getDs().find(User.class).delete();
getDs().save(entities);
Calendar instance = Calendar.getInstance();
instance.set(86, Calendar.MAY, 13);
Date date = instance.getTime();
updated = getDs().find(User.class).update(min("joined", date)).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
}
Aggregations