Search in sources :

Example 16 with UpdateOptions

use of dev.morphia.UpdateOptions in project morphia by mongodb.

the class TestDatastore method testUpdateWithCollation.

@Test
public void testUpdateWithCollation() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    final Update<FacebookUser> update = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).update(inc("loginCount"));
    UpdateResult results = update.execute();
    assertEquals(results.getModifiedCount(), 1);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    results = update.execute(new UpdateOptions().multi(true).collation(Collation.builder().locale("en").collationStrength(SECONDARY).build()));
    assertEquals(results.getModifiedCount(), 2);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 2);
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Aggregations

UpdateOptions (dev.morphia.UpdateOptions)16 Test (org.testng.annotations.Test)15 FindOptions (dev.morphia.query.FindOptions)7 Circle (dev.morphia.test.models.Circle)5 ObjectId (org.bson.types.ObjectId)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 Datastore (dev.morphia.Datastore)4 LocalDate (java.time.LocalDate)3 Date (java.util.Date)3 DeleteOptions (dev.morphia.DeleteOptions)2 UpdateOperators.currentDate (dev.morphia.query.experimental.updates.UpdateOperators.currentDate)2 User (dev.morphia.test.models.User)2 Versioned (dev.morphia.test.models.versioned.Versioned)2 Calendar (java.util.Calendar)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MongoWriteException (com.mongodb.MongoWriteException)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 Book (dev.morphia.test.models.Book)1 DocumentValidation (dev.morphia.test.models.DocumentValidation)1 FacebookUser (dev.morphia.test.models.FacebookUser)1