use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.
the class AbstractCrudTest method setUp.
@Before
public void setUp() {
assumeFalse(skipTest);
collectionHelper = new CollectionHelper<>(new DocumentCodec(), new MongoNamespace(databaseName, collectionName));
collectionHelper.create(collectionName, new CreateCollectionOptions(), WriteConcern.ACKNOWLEDGED);
createMongoClient(commandListener);
database = getDatabase(databaseName);
collection = database.getCollection(collectionName, BsonDocument.class);
helper = new JsonPoweredCrudTestHelper(description, database, collection);
if (!data.isEmpty()) {
List<BsonDocument> documents = new ArrayList<>();
for (BsonValue document : data) {
documents.add(document.asDocument());
}
if (documents.size() > 0) {
collectionHelper.insertDocuments(documents, WriteConcern.MAJORITY);
}
}
commandListener.reset();
}
use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.
the class AbstractClientSideEncryptionDeadlockTest method setUp.
@BeforeEach
public void setUp() throws IOException, URISyntaxException {
assumeTrue(serverVersionAtLeast(4, 2));
assumeTrue(isClientSideEncryptionTest());
MongoDatabase keyVaultDatabase = getMongoClient().getDatabase("keyvault");
MongoCollection<BsonDocument> dataKeysCollection = keyVaultDatabase.getCollection("datakeys", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
dataKeysCollection.drop();
dataKeysCollection.insertOne(bsonDocumentFromPath("external-key.json"));
MongoDatabase encryptedDatabase = getMongoClient().getDatabase("db");
MongoCollection<BsonDocument> encryptedCollection = encryptedDatabase.getCollection("coll", BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY);
encryptedCollection.drop();
encryptedDatabase.createCollection("coll", new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(new BsonDocument("$jsonSchema", bsonDocumentFromPath("external-schema.json")))));
kmsProviders = new HashMap<>();
Map<String, Object> localProviderMap = new HashMap<>();
localProviderMap.put("key", Base64.getDecoder().decode("Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZ" + "GJkTXVyZG9uSjFk"));
kmsProviders.put("local", localProviderMap);
ClientEncryption clientEncryption = ClientEncryptions.create(ClientEncryptionSettings.builder().keyVaultMongoClientSettings(getKeyVaultClientSettings(new TestCommandListener())).keyVaultNamespace("keyvault.datakeys").kmsProviders(kmsProviders).build());
cipherText = clientEncryption.encrypt(new BsonString("string0"), new EncryptOptions("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").keyAltName("local"));
clientEncryption.close();
}
use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.
the class UnifiedCrudHelper method executeCreateCollection.
public OperationResult executeCreateCollection(final BsonDocument operation) {
MongoDatabase database = entities.getDatabase(operation.getString("object").getValue());
BsonDocument arguments = operation.getDocument("arguments");
String collectionName = arguments.getString("collection").getValue();
ClientSession session = getSession(arguments);
CreateCollectionOptions options = new CreateCollectionOptions();
for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
switch(cur.getKey()) {
case "collection":
case "session":
break;
case "expireAfterSeconds":
options.expireAfter(cur.getValue().asNumber().longValue(), TimeUnit.SECONDS);
break;
case "timeseries":
options.timeSeriesOptions(createTimeSeriesOptions(cur.getValue().asDocument()));
break;
default:
throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
}
}
return resultOf(() -> {
if (session == null) {
database.createCollection(collectionName, options);
} else {
database.createCollection(session, collectionName, options);
}
return null;
});
}
use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.
the class CrudProseTest method testWriteErrorDetailsIsPropagated.
/**
* 2. WriteError.details exposes writeErrors[].errInfo
*/
@Test
public void testWriteErrorDetailsIsPropagated() {
assumeTrue(serverVersionAtLeast(3, 2));
getCollectionHelper().create(getCollectionName(), new CreateCollectionOptions().validationOptions(new ValidationOptions().validator(Filters.type("x", "string"))));
try {
collection.insertOne(new Document("x", 1));
fail("Should throw, as document doesn't match schema");
} catch (MongoWriteException e) {
// These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
// effective
assertTrue(e.getMessage().contains("Write error"));
assertNotNull(e.getError().getDetails());
if (serverVersionAtLeast(5, 0)) {
assertFalse(e.getError().getDetails().isEmpty());
}
}
try {
collection.insertMany(asList(new Document("x", 1)));
fail("Should throw, as document doesn't match schema");
} catch (MongoBulkWriteException e) {
// These assertions doesn't do exactly what's required by the specification, but it's simpler to implement and nearly as
// effective
assertTrue(e.getMessage().contains("Write errors"));
assertEquals(1, e.getWriteErrors().size());
if (serverVersionAtLeast(5, 0)) {
assertFalse(e.getWriteErrors().get(0).getDetails().isEmpty());
}
}
}
use of com.mongodb.client.model.CreateCollectionOptions in project spring-data-mongodb by spring-projects.
the class PerformanceTests method setupCollections.
private void setupCollections() {
MongoDatabase db = this.mongo.getDatabase(DATABASE_NAME);
for (String collectionName : COLLECTION_NAMES) {
MongoCollection<Document> collection = db.getCollection(collectionName);
collection.drop();
CreateCollectionOptions collectionOptions = new CreateCollectionOptions();
collectionOptions.capped(false);
collectionOptions.sizeInBytes(COLLECTION_SIZE);
db.createCollection(collectionName, collectionOptions);
collection.createIndex(new Document("firstname", -1));
collection.createIndex(new Document("lastname", -1));
}
}
Aggregations