@Signature public void createCollection(String name) { getWrappedObject().createCollection(name); }
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; }
db.createCollection(collectionName, co);
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(); } } } }
@Test public void testBypassDocumentValidation() { checkMinServerVersion(3.2); getDs().save(asList(new User("john doe", new Date()), new User("John Doe", new Date()))); MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME); database.getCollection("out_users").drop(); database.createCollection("out_users", new CreateCollectionOptions() .validationOptions(new ValidationOptions() .validator(Document.parse("{ \"age\" : { \"gte\" : 13 } }")))); try { getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class); fail("Document validation should have complained."); } catch (MongoCommandException e) { // expected } getDs() .createAggregation(User.class) .match(getDs().find(User.class).field("name").equal("john doe")) .out("out_users", User.class, builder() .bypassDocumentValidation(true) .build()); Assert.assertEquals(1, getAds().find("out_users", User.class).count()); }
MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME); database.getCollection("counts").drop(); database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
db.createCollection(collectionName, co);
protected void createCollection(String name) { MongoDatabase mongoDb = _dataContext.getMongoDb(); mongoDb.createCollection(name); MongoCollection<Document> collection = mongoDb.getCollection(name); _collections.put(name, collection); }
protected void createCollection(String name) { MongoDatabase mongoDb = _dataContext.getMongoDb(); mongoDb.createCollection(name); MongoCollection<Document> collection = mongoDb.getCollection(name); _collections.put(name, collection); }
/** {@inheritDoc} */ @Override public void createSchema() { if (!mongoClient.getDatabase(dbName) .listCollectionNames() .into(new HashSet<String>()) .contains(collectionName)) { mongoClient.getDatabase(dbName).createCollection(collectionName); } propertiesCollection = mongoClient.getDatabase(dbName).getCollection(collectionName); }
/** {@inheritDoc} */ @Override public void createSchema() { if (!mongoClient.getDatabase(dbName) .listCollectionNames() .into(new HashSet<String>()) .contains(collectionName)) { mongoClient.getDatabase(dbName).createCollection(collectionName); } eventsCollection = mongoClient.getDatabase(dbName).getCollection(collectionName); }
/** {@inheritDoc} */ @Override public void createSchema() { if (!mongoClient.getDatabase(dbName) .listCollectionNames() .into(new HashSet<String>()) .contains(collectionName)) { mongoClient.getDatabase(dbName).createCollection(collectionName); } featuresCollection = mongoClient.getDatabase(dbName).getCollection(collectionName); }
@Override public void createCollection(@NotNull String collectionName, @NotNull CreateCollectionOptions createCollectionOptions) { db.createCollection(collectionName, createCollectionOptions); }
@Override public void createCollection(@NotNull ClientSession clientSession, @NotNull String collectionName) { db.createCollection(clientSession, collectionName); }
@Override public void createCollection(@NotNull ClientSession clientSession, @NotNull String collectionName, @NotNull CreateCollectionOptions createCollectionOptions) { db.createCollection(clientSession, collectionName, createCollectionOptions); }
private MongoCollection<MongoBlob> initBlobCollection(MongoDatabase db) { if (stream(db.listCollectionNames().spliterator(), false) .noneMatch(COLLECTION_BLOBS::equals)) { db.createCollection(COLLECTION_BLOBS); } // override the read preference configured with the MongoDB URI // and use the primary as default. Reading a blob will still // try a secondary first and then fallback to the primary. return db.getCollection(COLLECTION_BLOBS, MongoBlob.class) .withCodecRegistry(CODEC_REGISTRY) .withReadPreference(primary()); }
private MongoCollection<MongoBlob> initBlobCollection(MongoDatabase db) { if (stream(db.listCollectionNames().spliterator(), false) .noneMatch(COLLECTION_BLOBS::equals)) { db.createCollection(COLLECTION_BLOBS); } // override the read preference configured with the MongoDB URI // and use the primary as default. Reading a blob will still // try a secondary first and then fallback to the primary. return db.getCollection(COLLECTION_BLOBS, MongoBlob.class) .withCodecRegistry(CODEC_REGISTRY) .withReadPreference(primary()); }
@Test public void testCreateCollectionAlreadyExists() throws Exception { db.createCollection("some-collection", new CreateCollectionOptions()); assertThatExceptionOfType(MongoCommandException.class) .isThrownBy(() -> db.createCollection("some-collection", new CreateCollectionOptions())) .withMessageContaining("Command failed with error 48 (NamespaceExists): 'a collection 'testdb.some-collection' already exists'"); }
private MongoCollection<Document> getCollection(String collectionName) { // first check if collection exists, if not then create a new collection with cappedSize if (!isCollectionExists(collectionName)) { CreateCollectionOptions option = new CreateCollectionOptions(); option.capped(true); option.maxDocuments(cappedMaxDocuments); option.sizeInBytes(cappedMaxSize); db.createCollection(collectionName, option); } return db.getCollection(collectionName); }
@Test public void testCreateCollection() throws Exception { String newCollectionName = "some-collection"; assertThat(toArray(db.listCollectionNames())).doesNotContain(newCollectionName); db.createCollection(newCollectionName, new CreateCollectionOptions()); assertThat(toArray(db.listCollectionNames())).contains(newCollectionName); }