Search in sources :

Example 6 with AggregateToCollectionOperation

use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.

the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarMergeDocument.

@DisplayName("Should build the expected AggregateOperation for $merge document")
@Test
void shouldBuildTheExpectedOperationsForDollarMergeDocument() {
    String collectionName = "collectionName";
    List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse(format("{'$merge': {into: '%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 7 with AggregateToCollectionOperation

use of com.mongodb.internal.operation.AggregateToCollectionOperation in project mongo-java-driver by mongodb.

the class AggregatePublisherImplTest method shouldBuildTheExpectedOperationsForDollarOutAsDocument.

@DisplayName("Should build the expected AggregateOperation for $out as document")
@Test
void shouldBuildTheExpectedOperationsForDollarOutAsDocument() {
    List<BsonDocument> pipeline = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse("{'$out': {s3: true}}"));
    TestOperationExecutor executor = createOperationExecutor(asList(null, null, null, null));
    AggregatePublisher<Document> publisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION);
    // default input should be as expected
    assertThrows(IllegalStateException.class, () -> Flux.from(publisher).blockFirst());
    // Should handle toCollection
    Publisher<Void> toCollectionPublisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.COLLECTION).toCollection();
    AggregateToCollectionOperation expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipeline, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    Flux.from(toCollectionPublisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    // Should handle database level
    toCollectionPublisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipeline, AggregationLevel.DATABASE).toCollection();
    Flux.from(toCollectionPublisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    // Should handle $out with namespace
    List<BsonDocument> pipelineWithNamespace = asList(BsonDocument.parse("{'$match': 1}"), BsonDocument.parse("{'$out': {db: 'db1', coll: 'coll1'}}"));
    toCollectionPublisher = new AggregatePublisherImpl<>(null, createMongoOperationPublisher(executor), pipelineWithNamespace, AggregationLevel.COLLECTION).toCollection();
    expectedOperation = new AggregateToCollectionOperation(NAMESPACE, pipelineWithNamespace, ReadConcern.DEFAULT, WriteConcern.ACKNOWLEDGED);
    Flux.from(toCollectionPublisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
}
Also used : AggregateToCollectionOperation(com.mongodb.internal.operation.AggregateToCollectionOperation) BsonDocument(org.bson.BsonDocument) 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