Search in sources :

Example 1 with Student

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());
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Grade(dev.morphia.test.models.Grade) Student(dev.morphia.test.models.Student) Test(org.testng.annotations.Test)

Example 2 with Student

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());
}
Also used : Datastore(dev.morphia.Datastore) Grade(dev.morphia.test.models.Grade) Student(dev.morphia.test.models.Student) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Aggregations

Datastore (dev.morphia.Datastore)2 Grade (dev.morphia.test.models.Grade)2 Student (dev.morphia.test.models.Student)2 Test (org.testng.annotations.Test)2 UpdateOptions (dev.morphia.UpdateOptions)1 FindOptions (dev.morphia.query.FindOptions)1