Giriş
Şu satırı dahil ederiz
import com.mongodb.client.model.CreateCollectionOptions;
Örnek
Şöyle yaparız
CreateCollectionOptions options = new CreateCollectionOptions(); ValidationOptions validationOptions = new ValidationOptions(); validationOptions.validator(BsonDocument.parse( "{\n" + " $jsonSchema: {\n" + " bsonType: \"object\",\n" + " title: \"Person Object Validation\",\n" + " required: [ \"firstName\", \"lastName\", \"birthYear\" ],\n" + " properties: {" + " \"firstName\": { \"bsonType\": \"string\" }\n" + " \"lastName\": { \"bsonType\": \"string\" }\n" + " \"employed\": { \"bsonType\": \"bool\" }\n" + " }\n" + " }\n" + " }\n" )); options.validationOptions(validationOptions); MongoClient client = MongoClients.create(...); String databaseName = "testDatabase"; String collectionName = "people"; MongoDatabase testDatabase = client.getDatabase(databaseName); testDatabase.createCollection(collectionName, options);
Hiç yorum yok:
Yorum Gönder