Search in sources :

Example 6 with BsonString

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

the class ListCollectionsOperation method asQueryDocument.

private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
    BsonDocument document = new BsonDocument();
    BsonDocument transformedFilter = null;
    if (filter != null) {
        if (filter.containsKey("name")) {
            if (!filter.isString("name")) {
                throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string");
            }
            transformedFilter = new BsonDocument();
            transformedFilter.putAll(filter);
            transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
        } else {
            transformedFilter = filter;
        }
    }
    BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
    BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter)));
    document.put("$query", query);
    if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
        document.put("$readPreference", readPreference.toDocument());
    }
    if (maxTimeMS > 0) {
        document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
    }
    return document;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonRegularExpression(org.bson.BsonRegularExpression)

Example 7 with BsonString

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

the class MapReduceWithInlineResultsOperation method getCommand.

private BsonDocument getCommand() {
    BsonDocument commandDocument = new BsonDocument("mapreduce", new BsonString(namespace.getCollectionName())).append("map", getMapFunction()).append("reduce", getReduceFunction()).append("out", new BsonDocument("inline", new BsonInt32(1))).append("query", asValueOrNull(getFilter())).append("sort", asValueOrNull(getSort())).append("finalize", asValueOrNull(getFinalizeFunction())).append("scope", asValueOrNull(getScope())).append("verbose", BsonBoolean.valueOf(isVerbose()));
    putIfNotZero(commandDocument, "limit", getLimit());
    putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS));
    putIfTrue(commandDocument, "jsMode", isJsMode());
    if (!readConcern.isServerDefault()) {
        commandDocument.put("readConcern", readConcern.asDocument());
    }
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 8 with BsonString

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

the class QueryBatchCursor method asGetMoreCommandDocument.

private BsonDocument asGetMoreCommandDocument() {
    BsonDocument document = new BsonDocument("getMore", new BsonInt64(serverCursor.getId())).append("collection", new BsonString(namespace.getCollectionName()));
    int batchSizeForGetMoreCommand = Math.abs(getNumberToReturn(limit, this.batchSize, count));
    if (batchSizeForGetMoreCommand != 0) {
        document.append("batchSize", new BsonInt32(batchSizeForGetMoreCommand));
    }
    if (maxTimeMS != 0) {
        document.append("maxTimeMS", new BsonInt64(maxTimeMS));
    }
    return document;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 9 with BsonString

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

the class DropIndexOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument commandDocument = new BsonDocument("dropIndexes", new BsonString(namespace.getCollectionName())).append("index", new BsonString(indexName));
    appendWriteConcernToCommand(writeConcern, commandDocument, description);
    return commandDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 10 with BsonString

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

the class FindAndDeleteOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument commandDocument = new BsonDocument("findandmodify", new BsonString(namespace.getCollectionName()));
    putIfNotNull(commandDocument, "query", getFilter());
    putIfNotNull(commandDocument, "fields", getProjection());
    putIfNotNull(commandDocument, "sort", getSort());
    putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS));
    commandDocument.put("remove", BsonBoolean.TRUE);
    if (serverIsAtLeastVersionThreeDotTwo(description) && writeConcern.isAcknowledged() && !writeConcern.isServerDefault()) {
        commandDocument.put("writeConcern", writeConcern.asDocument());
    }
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Aggregations

BsonString (org.bson.BsonString)168 BsonDocument (org.bson.BsonDocument)146 BsonInt32 (org.bson.BsonInt32)54 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)33 BsonValue (org.bson.BsonValue)31 Document (org.bson.Document)28 BsonInt64 (org.bson.BsonInt64)27 ArrayList (java.util.ArrayList)23 Map (java.util.Map)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 BsonDouble (org.bson.BsonDouble)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 Test (org.junit.jupiter.api.Test)11 List (java.util.List)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9 BsonBoolean (org.bson.BsonBoolean)9