use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestDatastore method testUpdateWithCollation.
@Test
public void testUpdateWithCollation() {
checkMinServerVersion(3.4);
getDs().getCollection(FacebookUser.class).drop();
getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
Query<FacebookUser> query = getDs().find(FacebookUser.class).field("username").equal("john doe");
UpdateOperations<FacebookUser> updateOperations = getDs().createUpdateOperations(FacebookUser.class).inc("loginCount");
UpdateResults results = getDs().update(query, updateOperations);
assertEquals(1, results.getUpdatedCount());
assertEquals(0, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount);
assertEquals(1, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount);
results = getDs().update(query, updateOperations, new UpdateOptions().multi(true).collation(Collation.builder().locale("en").collationStrength(CollationStrength.SECONDARY).build()));
assertEquals(2, results.getUpdatedCount());
assertEquals(1, getDs().find(FacebookUser.class).filter("id", 1).get().loginCount);
assertEquals(2, getDs().find(FacebookUser.class).filter("id", 2).get().loginCount);
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestFrontPageExample method testIt.
@Test
public void testIt() throws Exception {
getMorphia().map(Employee.class);
getDs().save(new Employee("Mister", "GOD", null, 0));
// get an employee without a manager
final Employee boss = getDs().find(Employee.class).field("manager").equal(null).get();
Assert.assertNotNull(boss);
final Key<Employee> key = getDs().save(new Employee("Scott", "Hernandez", getDs().getKey(boss), 150 * 1000));
Assert.assertNotNull(key);
final UpdateResults res = getDs().update(boss, getDs().createUpdateOperations(Employee.class).addToSet("underlings", //add Scott as an employee of his manager
key));
Assert.assertNotNull(res);
Assert.assertTrue("Should update existing document", res.getUpdatedExisting());
Assert.assertEquals("Should update one document", 1, res.getUpdatedCount());
// get Scott's boss
final Employee scottsBoss = getDs().find(Employee.class).filter("underlings", key).get();
Assert.assertNotNull(scottsBoss);
Assert.assertEquals(boss.id, scottsBoss.id);
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method testUpdateKeyList.
@Test
public void testUpdateKeyList() throws Exception {
final ContainsPicKey cpk = new ContainsPicKey();
cpk.name = "cpk one";
Datastore ds = getDs();
ds.save(cpk);
final Pic pic = new Pic();
pic.setName("fist again");
final Key<Pic> picKey = ds.save(pic);
cpk.keys = singletonList(picKey);
//test with Key<Pic>
final UpdateResults res = ds.update(ds.find(ContainsPicKey.class).filter("name", cpk.name), ds.createUpdateOperations(ContainsPicKey.class).set("keys", cpk.keys), new UpdateOptions());
assertThat(res.getUpdatedCount(), is(1));
//test reading the object.
final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).get();
assertThat(cpk2, is(notNullValue()));
assertThat(cpk.name, is(cpk2.name));
assertThat(cpk2.keys, hasItem(picKey));
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method testRemoveAllList.
@Test
public void testRemoveAllList() {
EntityLogs logs = new EntityLogs();
Date date = new Date();
logs.logs.addAll(asList(new EntityLog("log1", date), new EntityLog("log2", date), new EntityLog("log3", date), new EntityLog("log1", date), new EntityLog("log2", date), new EntityLog("log3", date)));
Datastore ds = getDs();
ds.save(logs);
UpdateOperations<EntityLogs> operations = ds.createUpdateOperations(EntityLogs.class).removeAll("logs", singletonList(new EntityLog("log3", date)));
UpdateResults results = ds.update(ds.find(EntityLogs.class), operations);
Assert.assertEquals(1, results.getUpdatedCount());
EntityLogs updated = ds.find(EntityLogs.class).get();
Assert.assertEquals(4, updated.logs.size());
for (int i = 0; i < 4; i++) {
Assert.assertEquals(new EntityLog("log" + ((i % 2) + 1), date), updated.logs.get(i));
}
}
use of org.mongodb.morphia.query.UpdateResults in project morphia by mongodb.
the class TestUpdateOps method testRemoveAllSingleValue.
@Test
public void testRemoveAllSingleValue() {
EntityLogs logs = new EntityLogs();
Date date = new Date();
logs.logs.addAll(asList(new EntityLog("log1", date), new EntityLog("log2", date), new EntityLog("log3", date), new EntityLog("log1", date), new EntityLog("log2", date), new EntityLog("log3", date)));
Datastore ds = getDs();
ds.save(logs);
UpdateOperations<EntityLogs> operations = ds.createUpdateOperations(EntityLogs.class).removeAll("logs", new EntityLog("log3", date));
UpdateResults results = ds.update(ds.find(EntityLogs.class), operations);
Assert.assertEquals(1, results.getUpdatedCount());
EntityLogs updated = ds.find(EntityLogs.class).get();
Assert.assertEquals(4, updated.logs.size());
for (int i = 0; i < 4; i++) {
Assert.assertEquals(new EntityLog("log" + ((i % 2) + 1), date), updated.logs.get(i));
}
}
Aggregations