use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue.
@Test
public void testMaxKeepsCurrentDocumentValueWhenThisIsLargerThanSuppliedValue() {
final ObjectId id = new ObjectId();
final double originalValue = 2D;
Datastore ds = getDs();
Query<Circle> query = ds.find(Circle.class).filter(eq("id", id));
assertInserted(query.update(setOnInsert(Map.of("radius", originalValue))).execute(new UpdateOptions().upsert(true)));
Assert.assertEquals(query.update(max("radius", 1D)).execute(new UpdateOptions().upsert(true)).getMatchedCount(), 1);
MatcherAssert.assertThat(ds.find(Circle.class).filter(eq("_id", id)).first().getRadius(), is(originalValue));
}
use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestArrayUpdates method testUpdatesWithArrayFilters.
@Test
public void testUpdatesWithArrayFilters() {
getMapper().map(Student.class, Grade.class);
final Datastore datastore = getDs();
datastore.ensureIndexes();
datastore.save(new Student(1L, new Grade(80, singletonMap("name", "Homework")), new Grade(90, singletonMap("name", "Test"))));
Query<Student> grade80 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 80));
Query<Student> grade90 = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 90));
assertNotNull(grade80.iterator().tryNext());
assertNotNull(grade90.iterator().tryNext());
Query<Student> student = datastore.find(Student.class).filter(eq("_id", 1L));
student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90)));
assertNull(grade80.iterator().tryNext());
assertNotNull(grade90.iterator().tryNext());
assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 85)).iterator().tryNext());
assertNotNull(grade90.iterator().tryNext());
student.update(inc("grades.$[elem].marks", 5)).execute(new UpdateOptions().arrayFilter(lt("elem.marks", 90).not()));
assertNull(grade90.iterator().tryNext());
assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.marks", 95)).iterator().tryNext());
}
use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testInsertWithRef.
@Test
public void testInsertWithRef() {
final Pic pic = new Pic();
pic.setName("fist");
final ObjectId picKey = getDs().save(pic).getId();
Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", picKey));
assertInserted(query.update(set("name", "A")).execute(new UpdateOptions().upsert(true)));
MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
getDs().find(ContainsPic.class).delete(new DeleteOptions().multi(true));
query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", pic));
assertInserted(query.update(set("name", "second")).execute(new UpdateOptions().upsert(true)));
MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
// test reading the object.
final ContainsPic cp = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
assertThat(cp, is(notNullValue()));
MatcherAssert.assertThat(cp.getName(), is("second"));
MatcherAssert.assertThat(cp.getPic(), is(notNullValue()));
MatcherAssert.assertThat(cp.getPic().getName(), is(notNullValue()));
MatcherAssert.assertThat(cp.getPic().getName(), is("fist"));
}
use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testSetOnInsertWhenUpdating.
@Test
public void testSetOnInsertWhenUpdating() {
ObjectId id = new ObjectId();
Query<Circle> query = getDs().find(Circle.class).filter(eq("id", id));
assertInserted(query.update(setOnInsert(Map.of("radius", 1D))).execute(new UpdateOptions().upsert(true)));
Assert.assertEquals(query.update(setOnInsert(Map.of("radius", 2D))).execute(new UpdateOptions().upsert(true)).getMatchedCount(), 1);
final Circle updatedCircle = getDs().find(Circle.class).filter(eq("_id", id)).first();
assertNotNull(updatedCircle);
Assert.assertEquals(updatedCircle.getRadius(), 1D, 0.1);
}
use of dev.morphia.UpdateOptions in project morphia by mongodb.
the class TestUpdateOperations method testMaxWithDates.
@Test
public void testMaxWithDates() {
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(max("joined", LocalDate.now())).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
getDs().find(User.class).delete();
getDs().save(entities);
updated = getDs().find(User.class).update(max("joined", Instant.now())).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
getDs().find(User.class).delete();
getDs().save(entities);
Calendar instance = Calendar.getInstance();
instance.set(2136, Calendar.MAY, 13);
Date date = instance.getTime();
updated = getDs().find(User.class).update(max("joined", date)).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
}
Aggregations