Search in sources :

Example 1 with InsertManyOptions

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

the class TestDatastore method testBulkInsert.

@Test
public void testBulkInsert() {
    MongoCollection collection = getDs().getCollection(TestEntity.class);
    this.getDs().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertManyOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
    assertEquals(collection.countDocuments(), 5);
    collection.drop();
    this.getDs().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertManyOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
    assertEquals(collection.countDocuments(), 5);
}
Also used : TestEntity(dev.morphia.test.models.TestEntity) MongoCollection(com.mongodb.client.MongoCollection) InsertManyOptions(dev.morphia.InsertManyOptions) Test(org.testng.annotations.Test)

Example 2 with InsertManyOptions

use of dev.morphia.InsertManyOptions 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 3 with InsertManyOptions

use of dev.morphia.InsertManyOptions 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)

Aggregations

InsertManyOptions (dev.morphia.InsertManyOptions)3 Test (org.testng.annotations.Test)3 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)2 MongoWriteException (com.mongodb.MongoWriteException)2 InsertOneOptions (dev.morphia.InsertOneOptions)2 FindOptions (dev.morphia.query.FindOptions)2 DocumentValidation (dev.morphia.test.models.DocumentValidation)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 MongoCollection (com.mongodb.client.MongoCollection)1 TestEntity (dev.morphia.test.models.TestEntity)1