Search in sources :

Example 1 with AggregateToCollectionOperation

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;
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) DBCollectionFindOptions(com.mongodb.client.model.DBCollectionFindOptions) BatchCursor(com.mongodb.internal.operation.BatchCursor) MapReduceBatchCursor(com.mongodb.internal.operation.MapReduceBatchCursor) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) MongoBatchCursorAdapter(com.mongodb.client.internal.MongoBatchCursorAdapter)

Example 2 with AggregateToCollectionOperation

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());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with AggregateToCollectionOperation

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());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with AggregateToCollectionOperation

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());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonDocument(org.bson.BsonDocument) FindOperation(com.mongodb.internal.operation.FindOperation) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with AggregateToCollectionOperation

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());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) MongoNamespace(com.mongodb.MongoNamespace) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)7 BsonDocument (org.bson.BsonDocument)7 Document (org.bson.Document)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 MongoNamespace (com.mongodb.MongoNamespace)5 BsonString (org.bson.BsonString)5 FindOperation (com.mongodb.internal.operation.FindOperation)2 MongoBatchCursorAdapter (com.mongodb.client.internal.MongoBatchCursorAdapter)1 DBCollectionFindOptions (com.mongodb.client.model.DBCollectionFindOptions)1 BatchCursor (com.mongodb.internal.operation.BatchCursor)1 MapReduceBatchCursor (com.mongodb.internal.operation.MapReduceBatchCursor)1 BsonInt32 (org.bson.BsonInt32)1 BsonValue (org.bson.BsonValue)1