use of dev.morphia.test.models.Student in project morphia by mongodb.
the class TestArrayUpdates method testStudents.
@Test
public void testStudents() {
getMapper().map(Student.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> testQuery = datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.data.name", "Test"));
assertNotNull(testQuery.iterator(new FindOptions().limit(1)).tryNext());
testQuery.update(set("grades.$.data.name", "Makeup Test")).execute();
assertNull(testQuery.iterator(new FindOptions().limit(1)).tryNext());
assertNotNull(datastore.find(Student.class).filter(eq("_id", 1L), eq("grades.data.name", "Makeup Test")).iterator(new FindOptions().limit(1)).tryNext());
}
use of dev.morphia.test.models.Student 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());
}
Aggregations