Search in sources :

Example 81 with FindOptions

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

the class TestMapping method testEmbeddedDocument.

@Test
public void testEmbeddedDocument() {
    getMapper().map(ContainsDocument.class);
    getDs().save(new ContainsDocument());
    assertNotNull(getDs().find(ContainsDocument.class).iterator(new FindOptions().limit(1)).next());
}
Also used : FindOptions(dev.morphia.query.FindOptions) ContainsDocument(dev.morphia.test.models.errors.ContainsDocument) Test(org.testng.annotations.Test)

Example 82 with FindOptions

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

the class TestDatastore method testCappedEntity.

@Test
public void testCappedEntity() {
    // given
    getMapper().map(CurrentStatus.class);
    getDs().ensureCaps();
    assertCapped(CurrentStatus.class, 1, 1048576);
    // when-then
    Query<CurrentStatus> query = getDs().find(CurrentStatus.class);
    getDs().save(new CurrentStatus("All Good"));
    assertEquals(query.count(), 1);
    getDs().save(new CurrentStatus("Kinda Bad"));
    assertEquals(query.count(), 1);
    assertTrue(query.iterator(new FindOptions().limit(1)).next().message.contains("Bad"));
    getDs().save(new CurrentStatus("Kinda Bad2"));
    assertEquals(query.count(), 1);
    getDs().save(new CurrentStatus("Kinda Bad3"));
    assertEquals(query.count(), 1);
    getDs().save(new CurrentStatus("Kinda Bad4"));
    assertEquals(query.count(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) CurrentStatus(dev.morphia.test.models.CurrentStatus) Test(org.testng.annotations.Test)

Example 83 with FindOptions

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

the class TestDocumentValidation method save.

@Test
public void save() {
    getMapper().map(DocumentValidation.class);
    getDs().enableDocumentValidation();
    try {
        getDs().save(new DocumentValidation("Harold", 8, new Date()));
        fail("Document validation should have complained");
    } catch (MongoWriteException e) {
    // expected
    }
    getDs().save(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("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()), new DocumentValidation("Harold", 8, new Date()));
    try {
        getDs().save(list);
        fail("Document validation should have complained");
    } catch (MongoBulkWriteException e) {
    // expected
    }
    getDs().save(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 84 with FindOptions

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

the class TestDocumentValidation method update.

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

Example 85 with FindOptions

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

the class TestVersioning method testVersionedUpsert.

@Test
public void testVersionedUpsert() {
    final Datastore datastore = getDs();
    datastore.find(Versioned.class).delete(new DeleteOptions().multi(true));
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("name", "Value 1"));
    query.update(set("name", "Value 3")).execute(new UpdateOptions().upsert(true));
    entity = datastore.find(Versioned.class).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) UpdateOptions(dev.morphia.UpdateOptions) 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