Search in sources :

Example 1 with ValidationOptions

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

the class MongoDatabaseImpl method createCollection.

@Override
@SuppressWarnings("deprecation")
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions, final SingleResultCallback<Void> callback) {
    CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).autoIndex(createCollectionOptions.isAutoIndex()).maxDocuments(createCollectionOptions.getMaxDocuments()).usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions())).collation(createCollectionOptions.getCollation());
    IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
    if (indexOptionDefaults.getStorageEngine() != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
    }
    ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
    if (validationOptions.getValidator() != null) {
        operation.validator(toBsonDocument(validationOptions.getValidator()));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    executor.execute(operation, callback);
}
Also used : CreateCollectionOperation(com.mongodb.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults)

Example 2 with ValidationOptions

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

the class CollectionHelper method create.

public void create(final String collectionName, final CreateCollectionOptions options) {
    drop(namespace);
    CreateCollectionOperation operation = new CreateCollectionOperation(namespace.getDatabaseName(), collectionName, WriteConcern.ACKNOWLEDGED).capped(options.isCapped()).sizeInBytes(options.getSizeInBytes()).autoIndex(options.isAutoIndex()).maxDocuments(options.getMaxDocuments());
    IndexOptionDefaults indexOptionDefaults = options.getIndexOptionDefaults();
    if (indexOptionDefaults.getStorageEngine() != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
    }
    ValidationOptions validationOptions = options.getValidationOptions();
    if (validationOptions.getValidator() != null) {
        operation.validator(toBsonDocument(validationOptions.getValidator()));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    operation.execute(getBinding());
}
Also used : CreateCollectionOperation(com.mongodb.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults)

Example 3 with ValidationOptions

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

the class MongoDatabaseImpl method createCollection.

@Override
@SuppressWarnings("deprecation")
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions) {
    CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).collation(createCollectionOptions.getCollation()).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).autoIndex(createCollectionOptions.isAutoIndex()).maxDocuments(createCollectionOptions.getMaxDocuments()).usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions()));
    IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
    if (indexOptionDefaults.getStorageEngine() != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
    }
    ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
    if (validationOptions.getValidator() != null) {
        operation.validator(toBsonDocument(validationOptions.getValidator()));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    executor.execute(operation);
}
Also used : CreateCollectionOperation(com.mongodb.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults)

Example 4 with ValidationOptions

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

the class DatastoreImpl method process.

void process(final MappedClass mc, final Validation validation) {
    if (validation != null) {
        String collectionName = mc.getCollectionName();
        CommandResult result = getDB().command(new BasicDBObject("collMod", collectionName).append("validator", parse(validation.value())).append("validationLevel", validation.level().getValue()).append("validationAction", validation.action().getValue()));
        if (!result.ok()) {
            if (result.getInt("code") == 26) {
                ValidationOptions options = new ValidationOptions().validator(parse(validation.value())).validationLevel(validation.level()).validationAction(validation.action());
                getDatabase().createCollection(collectionName, new CreateCollectionOptions().validationOptions(options));
            } else {
                result.throwOnError();
            }
        }
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) CommandResult(com.mongodb.CommandResult)

Example 5 with ValidationOptions

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

the class TestDocumentValidation method addValidation.

private MongoDatabase addValidation(final Document validator, final String collectionName) {
    ValidationOptions options = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.MODERATE).validationAction(ValidationAction.ERROR);
    MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
    database.getCollection(collectionName).drop();
    database.createCollection(collectionName, 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

ValidationOptions (com.mongodb.client.model.ValidationOptions)7 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)4 MongoDatabase (com.mongodb.client.MongoDatabase)3 IndexOptionDefaults (com.mongodb.client.model.IndexOptionDefaults)3 CreateCollectionOperation (com.mongodb.operation.CreateCollectionOperation)3 BsonDocument (org.bson.BsonDocument)3 MongoCommandException (com.mongodb.MongoCommandException)2 Test (org.junit.Test)2 BasicDBObject (com.mongodb.BasicDBObject)1 CommandResult (com.mongodb.CommandResult)1 Date (java.util.Date)1 Document (org.bson.Document)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1