Search in sources :

Example 1 with FindOperation

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;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) FindOperation(com.mongodb.internal.operation.FindOperation)

Example 2 with FindOperation

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());
}
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 3 with FindOperation

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

Aggregations

FindOperation (com.mongodb.internal.operation.FindOperation)3 BsonDocument (org.bson.BsonDocument)3 MongoNamespace (com.mongodb.MongoNamespace)2 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)2 BsonString (org.bson.BsonString)2 Document (org.bson.Document)2 DisplayName (org.junit.jupiter.api.DisplayName)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 BsonInt32 (org.bson.BsonInt32)1