use of dev.morphia.query.FindOptions 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.query.FindOptions 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.query.FindOptions in project morphia by mongodb.
the class TestArrayUpdates method testUpdates.
@Test
public void testUpdates() {
BatchData theBatch = new BatchData();
theBatch.files.add(new Files(0, "fileName1", "fileHash1"));
theBatch.files.add(new Files(0, "fileName2", "fileHash2"));
getDs().save(theBatch);
ObjectId id = theBatch.id;
theBatch = new BatchData();
theBatch.files.add(new Files(0, "fileName3", "fileHash3"));
theBatch.files.add(new Files(0, "fileName4", "fileHash4"));
getDs().save(theBatch);
ObjectId id2 = theBatch.id;
getDs().find(BatchData.class).filter(eq("_id", id), eq("files.fileName", "fileName1")).update(set("files.$.fileHash", "new hash")).execute();
BatchData data = getDs().find(BatchData.class).filter(eq("_id", id)).iterator(new FindOptions().limit(1)).tryNext();
assertEquals(data.files.get(0).fileHash, "new hash");
assertEquals(data.files.get(1).fileHash, "fileHash2");
data = getDs().find(BatchData.class).filter(eq("_id", id2)).iterator(new FindOptions().limit(1)).tryNext();
assertEquals(data.files.get(0).fileHash, "fileHash3");
assertEquals(data.files.get(1).fileHash, "fileHash4");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestDatastore method testFindAndModify.
@Test
public void testFindAndModify() {
getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
Query<FacebookUser> query = getDs().find(FacebookUser.class).filter(eq("username", "john doe"));
Modify<FacebookUser> modify = query.modify(inc("loginCount"));
assertEquals(modify.execute().loginCount, 0);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 1);
assertEquals(modify.execute(new ModifyOptions().returnDocument(AFTER)).loginCount, 2);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).first().loginCount, 0);
assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).first().loginCount, 2);
assertNull(getDs().find(FacebookUser.class).filter(eq("id", 3L), eq("username", "Jon Snow")).modify(inc("loginCount", 4)).execute(new ModifyOptions().returnDocument(BEFORE).upsert(true)));
FacebookUser user = getDs().find(FacebookUser.class).filter(eq("id", 3)).first();
assertEquals(user.loginCount, 4);
assertEquals(user.username, "Jon Snow");
FacebookUser results = getDs().find(FacebookUser.class).filter(eq("id", 4L), eq("username", "Ron Swanson")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(AFTER).upsert(true));
assertEquals(results.loginCount, 1);
assertEquals(results.username, "Ron Swanson");
user = getDs().find(FacebookUser.class).filter(eq("id", 4)).iterator(new FindOptions().limit(1)).next();
assertEquals(user.loginCount, 1);
assertEquals(user.username, "Ron Swanson");
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestLifecycles method testMultipleCallbackAnnotation.
@Test
public void testMultipleCallbackAnnotation() {
final SomeEntity entity = new SomeEntity();
Assert.assertFalse(entity.isPersistent());
getDs().save(entity);
Assert.assertTrue(entity.isPersistent());
final SomeEntity reloaded = getDs().find(SomeEntity.class).filter(eq("id", entity.getId())).iterator(new FindOptions().limit(1)).tryNext();
Assert.assertTrue(reloaded.isPersistent());
}
Aggregations