use of dev.morphia.Datastore in project morphia by mongodb.
the class TestVersioning method testUpdate.
@Test
public void testUpdate() {
Datastore ds = getDs();
List<VersionedType> initial = List.of(new VersionedType(), new VersionedType());
ds.save(initial);
UpdateResult results = ds.find(VersionedType.class).update(set("text", "some new value")).execute();
assertEquals(results.getModifiedCount(), 1);
List<VersionedType> postUpdate = ds.find(VersionedType.class).filter(eq("text", "some new value")).iterator(new FindOptions().sort(Sort.ascending("_id"))).toList();
for (int i = 0, postUpdateSize = postUpdate.size(); i < postUpdateSize; i++) {
final VersionedType versionedType = postUpdate.get(i);
assertEquals(versionedType.version, initial.get(i).version + 1);
}
}
use of dev.morphia.Datastore in project morphia by mongodb.
the class TestVersioning method testMethodMapping.
@Test
public void testMethodMapping() {
Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
datastore.getMapper().map(MethodMappedUser.class);
MethodMappedUser user = new MethodMappedUser();
assertEquals(user.getVersion(), null);
datastore.save(user);
assertEquals(user.getVersion(), Long.valueOf(1L));
}
use of dev.morphia.Datastore 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.Datastore 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.Datastore in project morphia by mongodb.
the class PullOperator method toTarget.
@Override
public OperationTarget toTarget(PathTarget pathTarget) {
return new OperationTarget(pathTarget, value()) {
@Override
public Object encode(Datastore datastore) {
DocumentWriter writer = new DocumentWriter(datastore.getMapper());
document(writer, () -> {
((Filter) getValue()).encode(datastore, writer, EncoderContext.builder().build());
});
return new Document(field(), writer.getDocument());
}
};
}
Aggregations