Search in sources :

Example 21 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class DatabaseAcceptanceTest method shouldSupportMaxNumberOfDocumentsInACappedCollection.

@Test
public void shouldSupportMaxNumberOfDocumentsInACappedCollection() {
    int maxDocuments = 5;
    database.createCollection(getCollectionName(), new CreateCollectionOptions().capped(true).sizeInBytes(40 * 1024).autoIndex(false).maxDocuments(maxDocuments));
    List<String> collections = database.listCollectionNames().into(new ArrayList<String>());
    assertThat(collections.contains(getCollectionName()), is(true));
    Document collStatsCommand = new Document("collStats", getCollectionName());
    Document collectionStatistics = database.runCommand(collStatsCommand, ReadPreference.primary());
    assertThat("max is set correctly in collection statistics", collectionStatistics.getInteger("max"), is(maxDocuments));
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 22 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions 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

CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)22 Test (org.junit.Test)17 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 Document (org.bson.Document)12 BasicDBObject (com.mongodb.BasicDBObject)8 MongoDatabase (com.mongodb.client.MongoDatabase)4 ValidationOptions (com.mongodb.client.model.ValidationOptions)4 DBObject (com.mongodb.DBObject)3 Ignore (org.junit.Ignore)3 MongoCommandException (com.mongodb.MongoCommandException)2 Calendar (java.util.Calendar)2 CommandResult (com.mongodb.CommandResult)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 TextSearchOptions (com.mongodb.client.model.TextSearchOptions)1 Date (java.util.Date)1 Bson (org.bson.conversions.Bson)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1