Search in sources :

Example 6 with MongoDatabase

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;
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) ValidationOptions(com.mongodb.client.model.ValidationOptions) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 7 with MongoDatabase

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()));
}
Also used : MongoCommandException(com.mongodb.MongoCommandException) Book(org.mongodb.morphia.aggregation.AggregationTest.Book) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) ValidationOptions(com.mongodb.client.model.ValidationOptions) CountResult(org.mongodb.morphia.aggregation.AggregationTest.CountResult) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.Test)

Example 8 with MongoDatabase

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;
}
Also used : Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) HashSet(java.util.HashSet)

Example 9 with MongoDatabase

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();
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) ImmutableList(com.google.common.collect.ImmutableList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 10 with MongoDatabase

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;
}
Also used : Document(org.bson.Document) Bson(org.bson.conversions.Bson) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

MongoDatabase (com.mongodb.client.MongoDatabase)34 Document (org.bson.Document)25 MongoClient (com.mongodb.MongoClient)14 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 MongoClientURI (com.mongodb.MongoClientURI)5 Bson (org.bson.conversions.Bson)4 ServerAddress (com.mongodb.ServerAddress)3 MongoCollection (com.mongodb.client.MongoCollection)3 CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)3 IndexOptions (com.mongodb.client.model.IndexOptions)3 DeleteResult (com.mongodb.client.result.DeleteResult)3 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)2 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 ImmutableList (com.google.common.collect.ImmutableList)2 MongoCommandException (com.mongodb.MongoCommandException)2 ValidationOptions (com.mongodb.client.model.ValidationOptions)2 Date (java.util.Date)2 Configuration (com.alibaba.datax.common.util.Configuration)1