Search in sources :

Example 41 with BsonInt32

use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.

the class AbstractExplainTest method testExplainOfAggregateWithOldResponseStructure.

@Test
public void testExplainOfAggregateWithOldResponseStructure() {
    // Aggregate explain is supported on earlier versions, but the structure of the response on which we're asserting in this test
    // changed radically in 4.2. So here we just assert that we got a non-error respinse
    assumeTrue(serverVersionAtLeast(3, 6));
    assumeTrue(serverVersionLessThan(4, 2));
    MongoCollection<BsonDocument> collection = client.getDatabase(getDefaultDatabaseName()).getCollection("explainTest", BsonDocument.class);
    collection.drop();
    collection.insertOne(new BsonDocument("_id", new BsonInt32(1)));
    AggregateIterable<BsonDocument> iterable = collection.aggregate(singletonList(Aggregates.match(Filters.eq("_id", 1))));
    Document explainDocument = iterable.explain();
    assertNotNull(explainDocument);
    explainDocument = iterable.explain(ExplainVerbosity.QUERY_PLANNER);
    assertNotNull(explainDocument);
    BsonDocument explainBsonDocument = iterable.explain(BsonDocument.class);
    assertNotNull(explainBsonDocument);
    explainBsonDocument = iterable.explain(BsonDocument.class, ExplainVerbosity.QUERY_PLANNER);
    assertNotNull(explainBsonDocument);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 42 with BsonInt32

use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.

the class QueryProtocol method asFindCommandDocument.

private BsonDocument asFindCommandDocument(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) {
    BsonDocument command = new BsonDocument(FIND_COMMAND_NAME, new BsonString(namespace.getCollectionName()));
    boolean isExplain = false;
    List<ByteBufBsonDocument> documents = ByteBufBsonDocument.createList(bsonOutput, firstDocumentPosition);
    ByteBufBsonDocument rawQueryDocument = documents.get(0);
    for (Map.Entry<String, BsonValue> cur : rawQueryDocument.entrySet()) {
        String commandFieldName = META_OPERATOR_TO_COMMAND_FIELD_MAP.get(cur.getKey());
        if (commandFieldName != null) {
            command.append(commandFieldName, cur.getValue());
        } else if (cur.getKey().equals("$explain")) {
            isExplain = true;
        }
    }
    if (command.size() == 1) {
        command.append("filter", rawQueryDocument);
    }
    if (documents.size() == 2) {
        command.append("projection", documents.get(1));
    }
    if (skip != 0) {
        command.append("skip", new BsonInt32(skip));
    }
    if (withLimitAndBatchSize) {
        if (limit != 0) {
            command.append("limit", new BsonInt32(limit));
        }
        if (batchSize != 0) {
            command.append("batchSize", new BsonInt32(batchSize));
        }
    }
    if (tailableCursor) {
        command.append("tailable", BsonBoolean.valueOf(tailableCursor));
    }
    if (noCursorTimeout) {
        command.append("noCursorTimeout", BsonBoolean.valueOf(noCursorTimeout));
    }
    if (oplogReplay) {
        command.append("oplogReplay", BsonBoolean.valueOf(oplogReplay));
    }
    if (awaitData) {
        command.append("awaitData", BsonBoolean.valueOf(awaitData));
    }
    if (partial) {
        command.append("allowPartialResults", BsonBoolean.valueOf(partial));
    }
    if (isExplain) {
        command = new BsonDocument(EXPLAIN_COMMAND_NAME, command);
    }
    return command;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 43 with BsonInt32

use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.

the class FindPublisherImplTest method shouldBuildTheExpectedOperation.

@DisplayName("Should build the expected FindOperation")
@Test
void shouldBuildTheExpectedOperation() {
    configureBatchCursor();
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
    FindPublisher<Document> publisher = new FindPublisherImpl<>(null, createMongoOperationPublisher(executor), new Document());
    FindOperation<Document> expectedOperation = new FindOperation<>(NAMESPACE, getDefaultCodecRegistry().get(Document.class)).batchSize(Integer.MAX_VALUE).retryReads(true).filter(new BsonDocument());
    // default input should be as expected
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    // Should apply settings
    publisher.filter(new Document("filter", 1)).sort(Sorts.ascending("sort")).projection(new Document("projection", 1)).maxTime(10, SECONDS).maxAwaitTime(20, SECONDS).batchSize(100).limit(100).skip(10).cursorType(CursorType.NonTailable).oplogReplay(false).noCursorTimeout(false).partial(false).collation(COLLATION).comment("my comment").hintString("a_1").min(new Document("min", 1)).max(new Document("max", 1)).returnKey(false).showRecordId(false).allowDiskUse(false);
    expectedOperation.allowDiskUse(false).batchSize(100).collation(COLLATION).comment("my comment").cursorType(CursorType.NonTailable).filter(new BsonDocument("filter", new BsonInt32(1))).hint(new BsonString("a_1")).limit(100).max(new BsonDocument("max", new BsonInt32(1))).maxAwaitTime(20000, MILLISECONDS).maxTime(10000, MILLISECONDS).min(new BsonDocument("min", new BsonInt32(1))).projection(new BsonDocument("projection", new BsonInt32(1))).returnKey(false).showRecordId(false).skip(10).secondaryOk(false).sort(new BsonDocument("sort", new BsonInt32(1)));
    configureBatchCursor();
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 44 with BsonInt32

use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.

the class ListDatabasesPublisherImplTest method shouldBuildTheExpectedOperation.

@DisplayName("Should build the expected ListDatabasesOperation")
@Test
void shouldBuildTheExpectedOperation() {
    configureBatchCursor();
    TestOperationExecutor executor = createOperationExecutor(asList(getBatchCursor(), getBatchCursor()));
    ListDatabasesPublisher<Document> publisher = new ListDatabasesPublisherImpl<>(null, createMongoOperationPublisher(executor));
    ListDatabasesOperation<Document> expectedOperation = new ListDatabasesOperation<>(getDefaultCodecRegistry().get(Document.class)).retryReads(true);
    // default input should be as expected
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
    // Should apply settings
    publisher.authorizedDatabasesOnly(true).filter(new Document("filter", 1)).maxTime(10, SECONDS).batchSize(100);
    expectedOperation.authorizedDatabasesOnly(true).filter(new BsonDocument("filter", new BsonInt32(1))).maxTime(10, SECONDS);
    configureBatchCursor();
    Flux.from(publisher).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getReadOperation());
    assertEquals(ReadPreference.primary(), executor.getReadPreference());
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 45 with BsonInt32

use of org.bson.BsonInt32 in project mongo-java-driver by mongodb.

the class MapReducePublisherImplTest method shouldBuildTheExpectedMapReduceToCollectionOperation.

@DisplayName("Should build the expected MapReduceToCollectionOperation")
@Test
void shouldBuildTheExpectedMapReduceToCollectionOperation() {
    MapReduceStatistics stats = Mockito.mock(MapReduceStatistics.class);
    TestOperationExecutor executor = createOperationExecutor(asList(stats, stats));
    com.mongodb.reactivestreams.client.MapReducePublisher<Document> publisher = new MapReducePublisherImpl<>(null, createMongoOperationPublisher(executor), MAP_FUNCTION, REDUCE_FUNCTION).collectionName(NAMESPACE.getCollectionName());
    MapReduceToCollectionOperation expectedOperation = new MapReduceToCollectionOperation(NAMESPACE, new BsonJavaScript(MAP_FUNCTION), new BsonJavaScript(REDUCE_FUNCTION), NAMESPACE.getCollectionName(), WriteConcern.ACKNOWLEDGED).verbose(true);
    // default input should be as expected
    Flux.from(publisher.toCollection()).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getWriteOperation());
    // Should apply settings
    publisher.batchSize(100).bypassDocumentValidation(true).collation(COLLATION).filter(new Document("filter", 1)).finalizeFunction(FINALIZE_FUNCTION).limit(999).maxTime(10, SECONDS).scope(new Document("scope", 1)).sort(Sorts.ascending("sort")).verbose(false);
    expectedOperation.collation(COLLATION).bypassDocumentValidation(true).filter(BsonDocument.parse("{filter: 1}")).finalizeFunction(new BsonJavaScript(FINALIZE_FUNCTION)).limit(999).maxTime(10, SECONDS).maxTime(10, SECONDS).scope(new BsonDocument("scope", new BsonInt32(1))).sort(new BsonDocument("sort", new BsonInt32(1))).verbose(false);
    Flux.from(publisher.toCollection()).blockFirst();
    assertOperationIsTheSameAs(expectedOperation, executor.getWriteOperation());
}
Also used : BsonInt32(org.bson.BsonInt32) MapReduceStatistics(com.mongodb.internal.operation.MapReduceStatistics) BsonDocument(org.bson.BsonDocument) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MapReduceToCollectionOperation(com.mongodb.internal.operation.MapReduceToCollectionOperation) BsonJavaScript(org.bson.BsonJavaScript) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

BsonInt32 (org.bson.BsonInt32)106 BsonDocument (org.bson.BsonDocument)91 BsonString (org.bson.BsonString)58 BsonArray (org.bson.BsonArray)26 Test (org.junit.Test)26 Document (org.bson.Document)23 BsonInt64 (org.bson.BsonInt64)20 Test (org.junit.jupiter.api.Test)16 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)13 List (java.util.List)9 BsonDouble (org.bson.BsonDouble)9 DisplayName (org.junit.jupiter.api.DisplayName)8 BsonNull (org.bson.BsonNull)7 MongoClientSettings (com.mongodb.MongoClientSettings)6 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)5 Map (java.util.Map)5 BsonBoolean (org.bson.BsonBoolean)5 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)4 ServerAddress (com.mongodb.ServerAddress)4