Search in sources :

Example 6 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestUpdateOperations method testUpsert.

@Test
public void testUpsert() {
    ContainsIntArray cIntArray = new ContainsIntArray();
    ContainsIntArray control = new ContainsIntArray();
    getDs().save(asList(cIntArray, control));
    Query<ContainsIntArray> query = getDs().find(ContainsIntArray.class);
    doUpdates(cIntArray, control, query.update(addToSet("values", 4)), new Integer[] { 1, 2, 3, 4 });
    doUpdates(cIntArray, control, query.update(addToSet("values", asList(4, 5))), new Integer[] { 1, 2, 3, 4, 5 });
    assertInserted(getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7 })).update(addToSet("values", 6)).execute(new UpdateOptions().upsert(true)));
    query = getDs().find(ContainsIntArray.class).filter(eq("values", new Integer[] { 4, 5, 7, 6 }));
    FindOptions options = new FindOptions().logQuery();
    assertNotNull(query.first(options), query.getLoggedQuery());
}
Also used : FindOptions(dev.morphia.query.FindOptions) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 7 with FindOptions

use of dev.morphia.query.FindOptions 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 8 with FindOptions

use of dev.morphia.query.FindOptions 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 9 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestDocumentValidation method insert.

@Test
public void insert() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getDs().insert(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    getDs().insert(new DocumentValidation("Harold", 8, new Date()), new InsertOneOptions().bypassDocumentValidation(true));
    Query<DocumentValidation> query = getDs().find(DocumentValidation.class).filter(eq("number", 8));
    Assert.assertNotNull(query.iterator(new FindOptions().limit(1)).tryNext());
    List<DocumentValidation> list = asList(new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("John", 8, new Date()), new DocumentValidation("Sarah", 8, new Date()), new DocumentValidation("Amy", 8, new Date()), new DocumentValidation("James", 8, new Date()));
    try {
        getDs().insert(list);
        fail("Document validation should have complained");
    } catch (MongoBulkWriteException e) {
    // expected
    }
    getDs().insert(list, new InsertManyOptions().bypassDocumentValidation(true));
    assertTrue(query.filter(eq("number", 8)).iterator().hasNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) DocumentValidation(dev.morphia.test.models.DocumentValidation) MongoWriteException(com.mongodb.MongoWriteException) InsertOneOptions(dev.morphia.InsertOneOptions) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) Date(java.util.Date) LocalDate(java.time.LocalDate) InsertManyOptions(dev.morphia.InsertManyOptions) Test(org.testng.annotations.Test)

Example 10 with FindOptions

use of dev.morphia.query.FindOptions in project morphia by mongodb.

the class TestMapping method testEmbeddedEntity.

@Test
public void testEmbeddedEntity() {
    getMapper().map(ContainsEmbeddedEntity.class);
    getDs().save(new ContainsEmbeddedEntity());
    final ContainsEmbeddedEntity ceeLoaded = getDs().find(ContainsEmbeddedEntity.class).iterator(new FindOptions().limit(1)).next();
    assertNotNull(ceeLoaded);
    assertNotNull(ceeLoaded.id);
    assertNotNull(ceeLoaded.cil);
    assertNull(ceeLoaded.cil.id);
}
Also used : FindOptions(dev.morphia.query.FindOptions) Test(org.testng.annotations.Test)

Aggregations

FindOptions (dev.morphia.query.FindOptions)118 Test (org.testng.annotations.Test)114 Point (com.mongodb.client.model.geojson.Point)16 Position (com.mongodb.client.model.geojson.Position)16 Document (org.bson.Document)16 Rectangle (dev.morphia.test.models.Rectangle)13 Datastore (dev.morphia.Datastore)12 UpdateResult (com.mongodb.client.result.UpdateResult)8 UpdateOptions (dev.morphia.UpdateOptions)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (dev.morphia.query.ValidationException)6 User (dev.morphia.test.models.User)5 ContainsPic (dev.morphia.test.query.TestQuery.ContainsPic)5 Pic (dev.morphia.test.query.TestQuery.Pic)5 LocalDate (java.time.LocalDate)5 Date (java.util.Date)5 DeleteOptions (dev.morphia.DeleteOptions)4 Query (dev.morphia.query.Query)4 DocumentValidation (dev.morphia.test.models.DocumentValidation)4 ContainsPic (dev.morphia.test.query.TestLegacyQuery.ContainsPic)4