Search in sources :

Example 41 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class MapReduceIterableImpl method asReadOperation.

@Override
public ReadOperation<BatchCursor<TResult>> asReadOperation() {
    if (inline) {
        ReadOperation<MapReduceBatchCursor<TResult>> operation = operations.mapReduce(mapFunction, reduceFunction, finalizeFunction, resultClass, filter, limit, maxTimeMS, jsMode, scope, sort, verbose, collation);
        return new WrappedMapReduceReadOperation<TResult>(operation);
    } else {
        getExecutor().execute(createMapReduceToCollectionOperation(), getReadConcern(), getClientSession());
        String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
        FindOptions findOptions = new FindOptions().collation(collation);
        Integer batchSize = getBatchSize();
        if (batchSize != null) {
            findOptions.batchSize(batchSize);
        }
        return operations.find(new MongoNamespace(dbName, collectionName), new BsonDocument(), resultClass, findOptions);
    }
}
Also used : FindOptions(com.mongodb.internal.client.model.FindOptions) MapReduceBatchCursor(com.mongodb.internal.operation.MapReduceBatchCursor) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace)

Example 42 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class AggregateIterableImpl method asReadOperation.

@Override
public ReadOperation<BatchCursor<TResult>> asReadOperation() {
    MongoNamespace outNamespace = getOutNamespace();
    if (outNamespace != null) {
        getExecutor().execute(operations.aggregateToCollection(pipeline, maxTimeMS, allowDiskUse, bypassDocumentValidation, collation, hint, hintString, comment, variables, aggregationLevel), getReadPreference(), getReadConcern(), getClientSession());
        FindOptions findOptions = new FindOptions().collation(collation);
        Integer batchSize = getBatchSize();
        if (batchSize != null) {
            findOptions.batchSize(batchSize);
        }
        return operations.find(outNamespace, new BsonDocument(), resultClass, findOptions);
    } else {
        return asAggregateOperation();
    }
}
Also used : FindOptions(com.mongodb.internal.client.model.FindOptions) BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace)

Example 43 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class ClientEncryptionImpl method createDataKey.

@Override
public BsonBinary createDataKey(final String kmsProvider, final DataKeyOptions dataKeyOptions) {
    BsonDocument dataKeyDocument = crypt.createDataKey(kmsProvider, dataKeyOptions);
    MongoNamespace namespace = new MongoNamespace(options.getKeyVaultNamespace());
    keyVaultClient.getDatabase(namespace.getDatabaseName()).getCollection(namespace.getCollectionName(), BsonDocument.class).withWriteConcern(WriteConcern.MAJORITY).insertOne(dataKeyDocument);
    return dataKeyDocument.getBinary("_id");
}
Also used : BsonDocument(org.bson.BsonDocument) MongoNamespace(com.mongodb.MongoNamespace)

Example 44 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class CommandMessage method getExtraElements.

private List<BsonElement> getExtraElements(final SessionContext sessionContext) {
    List<BsonElement> extraElements = new ArrayList<BsonElement>();
    extraElements.add(new BsonElement("$db", new BsonString(new MongoNamespace(getCollectionName()).getDatabaseName())));
    if (sessionContext.getClusterTime() != null) {
        extraElements.add(new BsonElement("$clusterTime", sessionContext.getClusterTime()));
    }
    if (sessionContext.hasSession() && responseExpected) {
        extraElements.add(new BsonElement("lsid", sessionContext.getSessionId()));
    }
    boolean firstMessageInTransaction = sessionContext.notifyMessageSent();
    assertFalse(sessionContext.hasActiveTransaction() && sessionContext.isSnapshot());
    if (sessionContext.hasActiveTransaction()) {
        checkServerVersionForTransactionSupport();
        extraElements.add(new BsonElement("txnNumber", new BsonInt64(sessionContext.getTransactionNumber())));
        if (firstMessageInTransaction) {
            extraElements.add(new BsonElement("startTransaction", BsonBoolean.TRUE));
            addReadConcernDocument(extraElements, sessionContext);
        }
        extraElements.add(new BsonElement("autocommit", BsonBoolean.FALSE));
    } else if (sessionContext.isSnapshot()) {
        addReadConcernDocument(extraElements, sessionContext);
    }
    if (serverApi != null) {
        addServerApiElements(extraElements);
    }
    if (readPreference != null) {
        if (!readPreference.equals(primary())) {
            extraElements.add(new BsonElement("$readPreference", readPreference.toDocument()));
        } else if (isDirectConnectionToReplicaSetMember()) {
            extraElements.add(new BsonElement("$readPreference", primaryPreferred().toDocument()));
        }
    }
    return extraElements;
}
Also used : BsonElement(org.bson.BsonElement) BsonInt64(org.bson.BsonInt64) BsonString(org.bson.BsonString) ArrayList(java.util.ArrayList) MongoNamespace(com.mongodb.MongoNamespace)

Example 45 with MongoNamespace

use of com.mongodb.MongoNamespace in project mongo-java-driver by mongodb.

the class UnifiedTest method addInitialData.

private void addInitialData() {
    for (BsonValue cur : initialData.getValues()) {
        BsonDocument curDataSet = cur.asDocument();
        CollectionHelper<BsonDocument> helper = new CollectionHelper<>(new BsonDocumentCodec(), new MongoNamespace(curDataSet.getString("databaseName").getValue(), curDataSet.getString("collectionName").getValue()));
        helper.create(WriteConcern.MAJORITY);
        BsonArray documentsArray = curDataSet.getArray("documents", new BsonArray());
        if (!documentsArray.isEmpty()) {
            helper.insertDocuments(documentsArray.stream().map(BsonValue::asDocument).collect(toList()), WriteConcern.MAJORITY);
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) CollectionHelper(com.mongodb.client.test.CollectionHelper) MongoNamespace(com.mongodb.MongoNamespace) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BsonValue(org.bson.BsonValue)

Aggregations

MongoNamespace (com.mongodb.MongoNamespace)55 BsonDocument (org.bson.BsonDocument)34 BsonString (org.bson.BsonString)21 Document (org.bson.Document)20 Before (org.junit.Before)17 MongoClientSettings (com.mongodb.MongoClientSettings)15 BsonValue (org.bson.BsonValue)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 Map (java.util.Map)10 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 DocumentCodec (org.bson.codecs.DocumentCodec)8 CollectionHelper (com.mongodb.client.test.CollectionHelper)7 Test (org.junit.jupiter.api.Test)7 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 Test (org.junit.Test)6 IndexOptions (com.mongodb.client.model.IndexOptions)5 AggregateToCollectionOperation (com.mongodb.internal.operation.AggregateToCollectionOperation)5 DisplayName (org.junit.jupiter.api.DisplayName)5