Search in sources :

Example 46 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class DBCollection method aggregate.

@SuppressWarnings("deprecation")
private Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference, final boolean returnCursorForOutCollection) {
    if (options == null) {
        throw new IllegalArgumentException("options can not be null");
    }
    List<BsonDocument> stages = preparePipeline(pipeline);
    BsonValue outCollection = stages.get(stages.size() - 1).get("$out");
    if (outCollection != null) {
        AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getWriteConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).bypassDocumentValidation(options.getBypassDocumentValidation()).collation(options.getCollation());
        try {
            executor.execute(operation);
            if (returnCursorForOutCollection) {
                return new DBCursor(database.getCollection(outCollection.asString().getValue()), new BasicDBObject(), new DBCollectionFindOptions().readPreference(primary()).collation(options.getCollation()));
            } else {
                return null;
            }
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    } else {
        AggregateOperation<DBObject> operation = new AggregateOperation<DBObject>(getNamespace(), stages, getDefaultDBObjectCodec()).readConcern(getReadConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).batchSize(options.getBatchSize()).useCursor(options.getOutputMode() == com.mongodb.AggregationOptions.OutputMode.CURSOR).collation(options.getCollation());
        BatchCursor<DBObject> cursor = executor.execute(operation, readPreference);
        return new MongoCursorAdapter(new MongoBatchCursorAdapter<DBObject>(cursor));
    }
}
Also used : AggregateToCollectionOperation(com.mongodb.operation.AggregateToCollectionOperation) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) AggregateOperation(com.mongodb.operation.AggregateOperation)

Example 47 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class MongoDatabaseImpl method createBsonDocumentList.

private List<BsonDocument> createBsonDocumentList(final List<? extends Bson> pipeline) {
    notNull("pipeline", pipeline);
    List<BsonDocument> bsonDocumentPipeline = new ArrayList<BsonDocument>(pipeline.size());
    for (Bson obj : pipeline) {
        if (obj == null) {
            throw new IllegalArgumentException("pipeline can not contain a null value");
        }
        bsonDocumentPipeline.add(obj.toBsonDocument(BsonDocument.class, codecRegistry));
    }
    return bsonDocumentPipeline;
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) Bson(org.bson.conversions.Bson)

Example 48 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class MongoDatabaseImpl method createCollection.

@Override
@SuppressWarnings("deprecation")
public void createCollection(final String collectionName, final CreateCollectionOptions createCollectionOptions) {
    CreateCollectionOperation operation = new CreateCollectionOperation(name, collectionName, writeConcern).collation(createCollectionOptions.getCollation()).capped(createCollectionOptions.isCapped()).sizeInBytes(createCollectionOptions.getSizeInBytes()).autoIndex(createCollectionOptions.isAutoIndex()).maxDocuments(createCollectionOptions.getMaxDocuments()).usePowerOf2Sizes(createCollectionOptions.isUsePowerOf2Sizes()).storageEngineOptions(toBsonDocument(createCollectionOptions.getStorageEngineOptions()));
    IndexOptionDefaults indexOptionDefaults = createCollectionOptions.getIndexOptionDefaults();
    if (indexOptionDefaults.getStorageEngine() != null) {
        operation.indexOptionDefaults(new BsonDocument("storageEngine", toBsonDocument(indexOptionDefaults.getStorageEngine())));
    }
    ValidationOptions validationOptions = createCollectionOptions.getValidationOptions();
    if (validationOptions.getValidator() != null) {
        operation.validator(toBsonDocument(validationOptions.getValidator()));
    }
    if (validationOptions.getValidationLevel() != null) {
        operation.validationLevel(validationOptions.getValidationLevel());
    }
    if (validationOptions.getValidationAction() != null) {
        operation.validationAction(validationOptions.getValidationAction());
    }
    executor.execute(operation);
}
Also used : CreateCollectionOperation(com.mongodb.operation.CreateCollectionOperation) BsonDocument(org.bson.BsonDocument) ValidationOptions(com.mongodb.client.model.ValidationOptions) IndexOptionDefaults(com.mongodb.client.model.IndexOptionDefaults)

Example 49 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class ServerDiscoveryAndMonitoringTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    for (BsonValue phase : getDefinition().getArray("phases")) {
        for (BsonValue response : phase.asDocument().getArray("responses")) {
            applyResponse(response.asArray());
        }
        BsonDocument outcome = phase.asDocument().getDocument("outcome");
        assertTopologyType(outcome.getString("topologyType").getValue());
        assertServers(outcome.getDocument("servers"));
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

Example 50 with BsonDocument

use of org.bson.BsonDocument in project mongo-java-driver by mongodb.

the class ServerSelectionSelectionTest method getServerSelector.

private ServerSelector getServerSelector() {
    if (definition.getString("operation", new BsonString("read")).getValue().equals("write")) {
        return new WritableServerSelector();
    } else {
        BsonDocument readPreferenceDefinition = definition.getDocument("read_preference");
        ReadPreference readPreference;
        if (readPreferenceDefinition.getString("mode").getValue().equals("Primary")) {
            readPreference = ReadPreference.valueOf("Primary");
        } else if (readPreferenceDefinition.containsKey("maxStalenessSeconds")) {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())), Math.round(readPreferenceDefinition.getNumber("maxStalenessSeconds").doubleValue() * 1000), TimeUnit.MILLISECONDS);
        } else {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())));
        }
        return new ReadPreferenceServerSelector(readPreference);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) WritableServerSelector(com.mongodb.selector.WritableServerSelector) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ReadPreferenceServerSelector(com.mongodb.selector.ReadPreferenceServerSelector)

Aggregations

BsonDocument (org.bson.BsonDocument)169 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)36 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 Document (org.bson.Document)7 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 ObjectId (org.bson.types.ObjectId)5