use of com.mongodb.client.MongoDatabase 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;
}
use of com.mongodb.client.MongoDatabase in project morphia by mongodb.
the class TestMapreduce method testBypassDocumentValidation.
@Test
public void testBypassDocumentValidation() {
checkMinServerVersion(3.4);
getDs().save(asList(new Book("The Banquet", "Dante", 2), new Book("Divine Comedy", "Dante", 1), new Book("Eclogues", "Dante", 2), new Book("The Odyssey", "Homer", 10), new Book("Iliad", "Homer", 10)));
Document validator = Document.parse("{ count : { $gt : '10' } }");
ValidationOptions validationOptions = new ValidationOptions().validator(validator).validationLevel(ValidationLevel.STRICT).validationAction(ValidationAction.ERROR);
MongoDatabase database = getMongoClient().getDatabase(TEST_DB_NAME);
database.getCollection("counts").drop();
database.createCollection("counts", new CreateCollectionOptions().validationOptions(validationOptions));
final String map = "function () { emit(this.author, 1); return; }";
final String reduce = "function (key, values) { return values.length }";
MapReduceOptions<CountResult> options = new MapReduceOptions<CountResult>().query(getDs().find(Book.class)).resultType(CountResult.class).outputType(OutputType.REPLACE).map(map).reduce(reduce);
try {
getDs().mapReduce(options);
fail("Document validation should have complained.");
} catch (MongoCommandException e) {
// expected
}
getDs().mapReduce(options.bypassDocumentValidation(true));
Assert.assertEquals(2, count(getDs().find(CountResult.class).iterator()));
}
use of com.mongodb.client.MongoDatabase in project presto by prestodb.
the class MongoSession method getTableMetadataNames.
private Set<String> getTableMetadataNames(String schemaName) throws TableNotFoundException {
MongoDatabase db = client.getDatabase(schemaName);
MongoCursor<Document> cursor = db.getCollection(schemaCollection).find().projection(new Document(TABLE_NAME_KEY, true)).iterator();
HashSet<String> names = new HashSet<>();
while (cursor.hasNext()) {
names.add((cursor.next()).getString(TABLE_NAME_KEY));
}
return names;
}
use of com.mongodb.client.MongoDatabase in project presto by prestodb.
the class MongoSession method guessTableFields.
private List<Document> guessTableFields(SchemaTableName schemaTableName) {
String schemaName = schemaTableName.getSchemaName();
String tableName = schemaTableName.getTableName();
MongoDatabase db = client.getDatabase(schemaName);
Document doc = db.getCollection(tableName).find().first();
if (doc == null) {
// no records at the collection
return ImmutableList.of();
}
ImmutableList.Builder<Document> builder = ImmutableList.builder();
for (String key : doc.keySet()) {
Object value = doc.get(key);
Optional<TypeSignature> fieldType = guessFieldType(value);
if (fieldType.isPresent()) {
Document metadata = new Document();
metadata.append(FIELDS_NAME_KEY, key);
metadata.append(FIELDS_TYPE_KEY, fieldType.get().toString());
metadata.append(FIELDS_HIDDEN_KEY, key.equals("_id") && fieldType.get().equals(OBJECT_ID.getTypeSignature()));
builder.add(metadata);
} else {
log.debug("Unable to guess field type from %s : %s", value == null ? "null" : value.getClass().getName(), value);
}
}
return builder.build();
}
use of com.mongodb.client.MongoDatabase in project camel by apache.
the class MongoDbProducer method calculateCollection.
private MongoCollection<Document> calculateCollection(Exchange exchange) {
// functionality at all
if (!endpoint.isDynamicity()) {
return endpoint.getMongoCollection().withWriteConcern(endpoint.getWriteConcern());
}
String dynamicDB = exchange.getIn().getHeader(DATABASE, String.class);
String dynamicCollection = exchange.getIn().getHeader(COLLECTION, String.class);
@SuppressWarnings("unchecked") List<Bson> dynamicIndex = exchange.getIn().getHeader(COLLECTION_INDEX, List.class);
MongoCollection<Document> dbCol;
if (dynamicDB == null && dynamicCollection == null) {
dbCol = endpoint.getMongoCollection().withWriteConcern(endpoint.getWriteConcern());
} else {
MongoDatabase db = calculateDb(exchange);
if (dynamicCollection == null) {
dbCol = db.getCollection(endpoint.getCollection(), Document.class);
} else {
dbCol = db.getCollection(dynamicCollection, Document.class);
// on the fly add index
if (dynamicIndex == null) {
endpoint.ensureIndex(dbCol, endpoint.createIndex());
} else {
endpoint.ensureIndex(dbCol, dynamicIndex);
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Dynamic database and/or collection selected: {}->{}", endpoint.getDatabase(), endpoint.getCollection());
}
return dbCol;
}
Aggregations