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