Example usage for com.mongodb.client.model ValidationLevel STRICT

List of usage examples for com.mongodb.client.model ValidationLevel STRICT

Introduction

In this page you can find the example usage for com.mongodb.client.model ValidationLevel STRICT.

Prototype

ValidationLevel STRICT

To view the source code for com.mongodb.client.model ValidationLevel STRICT.

Click Source Link

Document

Apply validation rules to all inserts and all updates.

Usage

From source file:org.flywaydb.core.internal.metadatatable.MongoMetaDataTable.java

License:Apache License

/**
 * Creates the metadata collection.//w w  w  . java 2 s .  co m
 *
 * @param dbName  The database where this metadata collection will be created.
 */
private void createMetadataCollection(String dbName) {
    LOG.info("In MongoDB " + dbName + ", creating Metadata collection: " + collectionName);
    // Database types used in the metadata collection schema.
    BasicDBObject dbInt = new BasicDBObject("$type", "int");
    BasicDBObject dbString = new BasicDBObject("$type", "string");
    BasicDBObject dbBool = new BasicDBObject("$type", "bool");
    BasicDBObject dbDate = new BasicDBObject("$type", "date");
    BasicDBObject dbNull = new BasicDBObject("$type", "null");
    BasicDBObject checksumTypeInt = new BasicDBObject(MetaDataDocument.CHECKSUM, dbInt);
    BasicDBObject checksumTypeNull = new BasicDBObject(MetaDataDocument.CHECKSUM, dbNull);
    BasicDBObject[] checksumType = { checksumTypeInt, checksumTypeNull };
    BasicDBObject versionTypeString = new BasicDBObject(MetaDataDocument.VERSION, dbString);
    BasicDBObject versionTypeNull = new BasicDBObject(MetaDataDocument.VERSION, dbNull);
    BasicDBObject[] versionType = { versionTypeString, versionTypeNull };
    // Database object for setting a valid mongo schema.
    BasicDBObject validSchema = new BasicDBObject().append(MetaDataDocument.INSTALLED_RANK, dbInt)
            .append("$or", versionType).append(MetaDataDocument.DESCRIPTION, dbString)
            .append(MetaDataDocument.TYPE, dbString).append(MetaDataDocument.SCRIPT, dbString)
            .append("$or", checksumType).append(MetaDataDocument.INSTALLED_BY, dbString)
            .append(MetaDataDocument.INSTALLED_ON, dbDate).append(MetaDataDocument.EXECUTION_TIME, dbInt)
            .append(MetaDataDocument.SUCCESS, dbBool);
    ValidationOptions validator = new ValidationOptions().validator(validSchema)
            .validationLevel(ValidationLevel.STRICT);
    CreateCollectionOptions collectionOptions = new CreateCollectionOptions().validationOptions(validator);

    Boolean dbExists = MongoDatabaseUtil.exists(client, dbName);
    if (dbExists) {
        LOG.debug("Database " + dbName + " already exists. Skipping database creation.");
    } else {
        LOG.info("Creating Mongo database " + dbName + " ...");
    }
    mongoDatabase.createCollection(collectionName, collectionOptions);
    LOG.debug("Metadata collection " + collectionName + " created.");
    this.metadataCollection = mongoDatabase.getCollection(collectionName, BasicDBObject.class);
    if (!dbExists)
        addSchemaMarker();
}

From source file:org.springframework.data.mongodb.core.CollectionOptions.java

License:Apache License

/**
 * Create new {@link CollectionOptions} with already given settings and {@code validationLevel} set to
 * {@link ValidationLevel#STRICT}./*from ww w  .ja  v a 2  s .  c o  m*/
 *
 * @return new {@link CollectionOptions}.
 * @since 2.1
 */
public CollectionOptions strictValidation() {
    return schemaValidationLevel(ValidationLevel.STRICT);
}