use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.
the class DBCollection method aggregate.
/**
* Method implements aggregation framework.
*
* @param pipeline operations to be performed in the aggregation pipeline
* @param options options to apply to the aggregation
* @param readPreference {@link ReadPreference} to be used for this operation
* @return the aggregation operation's result set
* @mongodb.driver.manual core/aggregation-pipeline/ Aggregation
* @mongodb.server.release 2.2
*/
public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options, final ReadPreference readPreference) {
Cursor result;
notNull("options", options);
List<BsonDocument> stages = preparePipeline(pipeline);
BsonValue outCollection = stages.get(stages.size() - 1).get("$out");
if (outCollection != null) {
AggregateToCollectionOperation operation = new AggregateToCollectionOperation(getNamespace(), stages, getReadConcern(), getWriteConcern()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).bypassDocumentValidation(options.getBypassDocumentValidation()).collation(options.getCollation());
try {
executor.execute(operation, getReadPreference(), getReadConcern());
result = new DBCursor(database.getCollection(outCollection.asString().getValue()), new BasicDBObject(), new DBCollectionFindOptions().readPreference(primary()).collation(options.getCollation()));
} catch (MongoWriteConcernException e) {
throw createWriteConcernException(e);
}
} else {
AggregateOperation<DBObject> operation = new AggregateOperation<DBObject>(getNamespace(), stages, getDefaultDBObjectCodec()).maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS).allowDiskUse(options.getAllowDiskUse()).batchSize(options.getBatchSize()).collation(options.getCollation()).retryReads(retryReads);
BatchCursor<DBObject> cursor1 = executor.execute(operation, readPreference, getReadConcern());
result = new MongoCursorAdapter(new MongoBatchCursorAdapter<DBObject>(cursor1));
}
return result;
}
use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.
the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarOutWithHintString.
@DisplayName("Should build the expected AggregateOperation for $out with hint string")
@Test
void shouldBuildTheExpectedOperationsForDollarOutWithHintString() {
String collectionName = "collectionName";
List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$out': '%s'}", collectionName)));
MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
publisher.hintString("x_1");
expectedOperation.hint(new BsonString("x_1"));
Flux.from(publisher).blockFirst();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
}
use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.
the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarMergeString.
@DisplayName("Should build the expected AggregateOperation for $merge string")
@Test
void shouldBuildTheExpectedOperationsForDollarMergeString() {
String collectionName = "collectionName";
MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$merge': '%s'}", collectionName)));
TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
// default input should be as expected
Flux.from(publisher).blockFirst();
VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
FindOperation<Document> expectedFindOperation = new FindOperation<>(collectionNamespace, getDefaultCodecRegistry().get(Document.class)).filter(new BsonDocument()).batchSize(Integer.MAX_VALUE).retryReads(true);
assertOperationIsTheSameAs(expectedFindOperation, operation.getCursorReadOperation());
}
use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.
the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarOut.
@DisplayName("Should build the expected AggregateOperation for $out")
@Test
void shouldBuildTheExpectedOperationsForDollarOut() {
String collectionName = "collectionName";
List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$out': '%s'}", collectionName)));
MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
// default input should be as expected
Flux.from(publisher).blockFirst();
VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
// Should apply settings
publisher.allowDiskUse(true).batchSize(// Used in Find
100).bypassDocumentValidation(true).collation(COLLATION).comment("my comment").hint(BsonDocument.parse("{a: 1}")).maxAwaitTime(20, // Ignored on $out
SECONDS).maxTime(10, SECONDS);
expectedOperation.allowDiskUse(true).bypassDocumentValidation(true).collation(COLLATION).comment("my comment").hint(BsonDocument.parse("{a: 1}")).maxTime(10, SECONDS);
Flux.from(publisher).blockFirst();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
FindOperation<Document> expectedFindOperation = new FindOperation<>(collectionNamespace, getDefaultCodecRegistry().get(Document.class)).batchSize(100).collation(COLLATION).filter(new BsonDocument()).maxAwaitTime(0, SECONDS).maxTime(0, SECONDS).retryReads(true);
assertOperationIsTheSameAs(expectedFindOperation, operation.getCursorReadOperation());
// Should handle database level aggregations
publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.DATABASE);
expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
Flux.from(publisher).blockFirst();
operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
// Should handle toCollection
publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
// default input should be as expected
Flux.from(publisher.toCollection()).blockFirst();
assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
}
use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.
the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarOutWithHintPlusHintString.
@DisplayName("Should build the expected AggregateOperation for $out when both hint and hint string are set")
@Test
void shouldBuildTheExpectedOperationsForDollarOutWithHintPlusHintString() {
String collectionName = "collectionName";
List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$out': '%s'}", collectionName)));
MongoNamespace collectionNamespace = new MongoNamespace(NAMESPACE.getDatabaseName(), collectionName);
TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor(), getBatchCursor(), null));
AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
publisher.hint(new Document("x", 1)).hintString("x_1");
expectedOperation.hint(new BsonDocument("x", new BsonInt32(1)));
Flux.from(publisher).blockFirst();
assertEquals(ReadPreference.primary(), executor.getReadPreference());
VoidReadOperationThenCursorReadOperation operation = (VoidReadOperationThenCursorReadOperation) executor.getReadOperation();
assertOperationIsTheSameAs(expectedOperation, operation.getReadOperation());
}
Aggregations