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);
}
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);
}
}
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;
}
Aggregations