Search in sources :

Example 11 with MongoCommandException

use of com.mongodb.MongoCommandException in project morphia by mongodb.

the class AggregationTest method testBypassDocumentValidation.

@Test
public void testBypassDocumentValidation() {
    checkMinServerVersion(3.2);
    getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date())));
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection("out_users").drop();
    database.createCollection("out_users", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(Document.parse("{ age : { gte : 13 } }"))));
    try {
        getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class);
        fail("Document validation should have complained.");
    } catch (MongoCommandException e) {
    // expected
    }
    getDs().createAggregation(User.class).match(getDs().find(User.class).field("name").equal("john doe")).out("out_users", User.class, builder().bypassDocumentValidation(true).build());
    Assert.assertEquals(1, getAds().find("out_users", User.class).count());
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Aggregations

MongoCommandException (com.mongodb.MongoCommandException)11 Document (org.bson.Document)4 FutureResultCallback (com.mongodb.async.FutureResultCallback)3 BsonDocument (org.bson.BsonDocument)3 Test (org.junit.Test)3 MongoSecurityException (com.mongodb.MongoSecurityException)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Version (com.github.zafarkhaja.semver.Version)1 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 MongoQueryException (com.mongodb.MongoQueryException)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1 DocumentValidation (org.mongodb.morphia.entities.DocumentValidation)1