Search in sources :

Example 11 with UpdateResults

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);
}
Also used : UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 12 with UpdateResults

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);
}
Also used : UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 13 with UpdateResults

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));
}
Also used : Pic(org.mongodb.morphia.query.TestQuery.Pic) ContainsPic(org.mongodb.morphia.query.TestQuery.ContainsPic) UpdateResults(org.mongodb.morphia.query.UpdateResults) Test(org.junit.Test)

Example 14 with UpdateResults

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));
    }
}
Also used : UpdateResults(org.mongodb.morphia.query.UpdateResults) Date(java.util.Date) Test(org.junit.Test)

Example 15 with UpdateResults

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));
    }
}
Also used : UpdateResults(org.mongodb.morphia.query.UpdateResults) Date(java.util.Date) Test(org.junit.Test)

Aggregations

UpdateResults (org.mongodb.morphia.query.UpdateResults)16 Test (org.junit.Test)10 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 DBCollection (com.mongodb.DBCollection)3 LinkedHashMap (java.util.LinkedHashMap)3 MappedClass (org.mongodb.morphia.mapping.MappedClass)3 MappedField (org.mongodb.morphia.mapping.MappedField)3 WriteResult (com.mongodb.WriteResult)2 Date (java.util.Date)2 Datastore (org.mongodb.morphia.Datastore)2 MongoClient (com.mongodb.MongoClient)1 DBCollectionUpdateOptions (com.mongodb.client.model.DBCollectionUpdateOptions)1 ConcurrentModificationException (java.util.ConcurrentModificationException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ObjectId (org.bson.types.ObjectId)1 Ignore (org.junit.Ignore)1 Morphia (org.mongodb.morphia.Morphia)1