Search in sources :

Example 1 with ModifyOptions

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

the class TestDatastore method testFindAndModifyWithOptions.

@Test
public void testFindAndModifyWithOptions() {
    getDs().save(asList(new FacebookUser(1, "John Doe"), new FacebookUser(2, "john doe")));
    FacebookUser result = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).modify(inc("loginCount")).execute();
    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);
    assertEquals(result.loginCount, 0);
    result = getDs().find(FacebookUser.class).filter(eq("username", "john doe")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(BEFORE).collation(Collation.builder().locale("en").collationStrength(SECONDARY).build()));
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 1)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    assertEquals(result.loginCount, 0);
    assertEquals(getDs().find(FacebookUser.class).filter(eq("id", 2)).iterator(new FindOptions().limit(1)).next().loginCount, 1);
    result = getDs().find(FacebookUser.class).filter(eq("id", 3L), eq("username", "Jon Snow")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(BEFORE).upsert(true));
    assertNull(result);
    FacebookUser user = getDs().find(FacebookUser.class).filter(eq("id", 3)).iterator(new FindOptions().limit(1)).next();
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Jon Snow");
    result = getDs().find(FacebookUser.class).filter(eq("id", 4L), eq("username", "Ron Swanson")).modify(inc("loginCount")).execute(new ModifyOptions().returnDocument(AFTER).upsert(true));
    assertNotNull(result);
    user = getDs().find(FacebookUser.class).filter(eq("id", 4)).iterator(new FindOptions().limit(1)).next();
    assertEquals(result.loginCount, 1);
    assertEquals(result.username, "Ron Swanson");
    assertEquals(user.loginCount, 1);
    assertEquals(user.username, "Ron Swanson");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 2 with ModifyOptions

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

the class TestDocumentValidation method findAndModify.

@Test
public void findAndModify() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    getDs().save(new DocumentValidation("Harold", 100, new Date()));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class);
    ModifyOptions options = new ModifyOptions().bypassDocumentValidation(false);
    Modify<DocumentValidation> modify = query.modify(set("number", 5));
    try {
        modify.execute(options);
        fail("Document validation should have complained");
    } catch (MongoCommandException e) {
    // expected
    }
    options.bypassDocumentValidation(true);
    modify.execute(options);
    Assert.assertNotNull(query.filter(eq("number", 5)).iterator(new FindOptions().limit(1)).next());
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoCommandException(com.mongodb.MongoCommandException) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 3 with ModifyOptions

use of dev.morphia.ModifyOptions 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");
}
Also used : FindOptions(dev.morphia.query.FindOptions) ModifyOptions(dev.morphia.ModifyOptions) FacebookUser(dev.morphia.test.models.FacebookUser) Test(org.testng.annotations.Test)

Example 4 with ModifyOptions

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

the class TestVersioning method testFindAndModify.

@Test
public void testFindAndModify() {
    final Datastore datastore = getDs();
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("name", "Value 1"));
    entity = query.modify(set("name", "Value 3")).execute(new ModifyOptions().returnDocument(ReturnDocument.AFTER).upsert(true));
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 1);
    assertNotNull(entity.getId());
}
Also used : ModifyOptions(dev.morphia.ModifyOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) Test(org.testng.annotations.Test)

Aggregations

ModifyOptions (dev.morphia.ModifyOptions)4 Test (org.testng.annotations.Test)4 FindOptions (dev.morphia.query.FindOptions)3 FacebookUser (dev.morphia.test.models.FacebookUser)2 MongoCommandException (com.mongodb.MongoCommandException)1 Datastore (dev.morphia.Datastore)1 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)1 DocumentValidation (dev.morphia.test.models.DocumentValidation)1 Versioned (dev.morphia.test.models.versioned.Versioned)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1