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));
}
}
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;
}
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);
}
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"));
}
}
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);
}
}
Aggregations