use of com.mongodb.internal.operation.FindOperation in project mongo-java-driver by mongodb.
the class CollectionHelper method find.
public <D> List<D> find(final Codec<D> codec) {
BatchCursor<D> cursor = new FindOperation<D>(namespace, codec).sort(new BsonDocument("_id", new BsonInt32(1))).execute(getBinding());
List<D> results = new ArrayList<D>();
while (cursor.hasNext()) {
results.addAll(cursor.next());
}
return results;
}
use of com.mongodb.internal.operation.FindOperation 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.FindOperation 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());
}
Aggregations