Search in sources :

Example 36 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project spring-data-mongodb by spring-projects.

the class MongoJsonSchemaTests method writeSchemaManually.

// DATAMONGO-1835
@Test
public void writeSchemaManually() {
    MongoJsonSchema schema = // 
    MongoJsonSchema.builder().required("firstname", // 
    "lastname").properties(// 
    JsonSchemaProperty.string("firstname").possibleValues("luke", "han").maxLength(10), // 
    JsonSchemaProperty.object("address").properties(JsonSchemaProperty.string("postCode").minLength(4).maxLength(5))).build();
    Document $jsonSchema = new MongoJsonSchemaMapper(template.getConverter()).mapSchema(schema.toDocument(), Person.class);
    ValidationOptions options = new ValidationOptions();
    options.validationLevel(ValidationLevel.MODERATE);
    options.validationAction(ValidationAction.ERROR);
    options.validator($jsonSchema);
    CreateCollectionOptions cco = new CreateCollectionOptions();
    cco.validationOptions(options);
    MongoDatabase db = template.getDb();
    db.createCollection("persons", cco);
    Document fromDb = readSchemaFromDatabase("persons");
    assertThat(fromDb).isEqualTo($jsonSchema);
}
Also used : MongoJsonSchemaMapper(org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 37 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project gora by apache.

the class MongoStore method createSchema.

/**
 * Create a new collection in MongoDB if necessary.
 */
@Override
public void createSchema() throws GoraException {
    if (mongoClientDB == null)
        throw new GoraException("Impossible to create the schema as no database has been selected.");
    if (schemaExists()) {
        return;
    }
    try {
        // If initialized create the collection
        CreateCollectionOptions opts = new CreateCollectionOptions();
        String name = mapping.getCollectionName();
        mongoClientDB.createCollection(name, opts);
        mongoClientColl = mongoClientDB.getCollection(name);
        LOG.debug("Collection {} has been created for Mongo database {}.", new Object[] { name, mongoClientDB.getName() });
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 38 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project morphia by mongodb.

the class TestDocumentValidation method addValidation.

private MongoDatabase addValidation(Document validator) {
    ValidationOptions options = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.MODERATE).validationAction(ValidationAction.ERROR);
    MongoDatabase database = getMongoClient().getDatabase(TestBase.TEST_DB_NAME);
    database.getCollection("validation").drop();
    database.createCollection("validation", new CreateCollectionOptions().validationOptions(options));
    return database;
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)38 Document (org.bson.Document)19 Test (org.junit.Test)19 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 ValidationOptions (com.mongodb.client.model.ValidationOptions)11 BasicDBObject (com.mongodb.BasicDBObject)8 MongoDatabase (com.mongodb.client.MongoDatabase)8 BsonDocument (org.bson.BsonDocument)5 MongoNamespace (com.mongodb.MongoNamespace)4 DBObject (com.mongodb.DBObject)3 MongoCommandException (com.mongodb.MongoCommandException)3 BsonString (org.bson.BsonString)3 BsonValue (org.bson.BsonValue)3 Ignore (org.junit.Ignore)3 Test (org.junit.jupiter.api.Test)3 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Before (org.junit.Before)2