Search in sources :

Example 46 with BsonString

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

the class MapReduceWithInlineResultsOperation method getCommand.

private BsonDocument getCommand(final SessionContext sessionContext, final int maxWireVersion) {
    BsonDocument commandDocument = new BsonDocument("mapreduce", new BsonString(namespace.getCollectionName())).append("map", getMapFunction()).append("reduce", getReduceFunction()).append("out", new BsonDocument("inline", new BsonInt32(1)));
    putIfNotNull(commandDocument, "query", getFilter());
    putIfNotNull(commandDocument, "sort", getSort());
    putIfNotNull(commandDocument, "finalize", getFinalizeFunction());
    putIfNotNull(commandDocument, "scope", getScope());
    putIfTrue(commandDocument, "verbose", isVerbose());
    appendReadConcernToCommand(sessionContext, maxWireVersion, commandDocument);
    putIfNotZero(commandDocument, "limit", getLimit());
    putIfNotZero(commandDocument, "maxTimeMS", getMaxTime(MILLISECONDS));
    putIfTrue(commandDocument, "jsMode", isJsMode());
    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 47 with BsonString

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

the class RenameCollectionOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument commandDocument = new BsonDocument("renameCollection", new BsonString(originalNamespace.getFullName())).append("to", new BsonString(newNamespace.getFullName())).append("dropTarget", BsonBoolean.valueOf(dropTarget));
    appendWriteConcernToCommand(writeConcern, commandDocument, description);
    return commandDocument;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 48 with BsonString

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

the class UnifiedCrudHelper method executeDistinct.

OperationResult executeDistinct(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    ClientSession session = getSession(arguments);
    BsonString fieldName = arguments.getString("fieldName");
    DistinctIterable<BsonValue> iterable = session == null ? collection.distinct(fieldName.getValue(), BsonValue.class) : collection.distinct(session, fieldName.getValue(), BsonValue.class);
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "fieldName":
            case "session":
                break;
            case "filter":
                iterable.filter(cur.getValue().asDocument());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> new BsonArray(iterable.into(new ArrayList<>())));
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ClientSession(com.mongodb.client.ClientSession) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 49 with BsonString

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

the class UnifiedGridFSHelper method executeDownload.

public OperationResult executeDownload(final BsonDocument operation) {
    GridFSBucket bucket = entities.getBucket(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    BsonValue id = arguments.get("id");
    if (arguments.size() > 1) {
        throw new UnsupportedOperationException("Unexpected arguments");
    }
    requireNonNull(id);
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bucket.downloadToStream(id, baos);
        return OperationResult.of(new BsonString(HexUtils.toHex(baos.toByteArray())));
    } catch (Exception e) {
        return OperationResult.of(e);
    }
}
Also used : GridFSBucket(com.mongodb.client.gridfs.GridFSBucket) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BsonValue(org.bson.BsonValue)

Example 50 with BsonString

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

the class GetMoreProtocol method asGetMoreCommandResponseDocument.

private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
        responseBuffers.reset();
        rawResultDocuments = ByteBufBsonDocument.createList(responseBuffers);
    }
    BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
    return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Aggregations

BsonString (org.bson.BsonString)178 BsonDocument (org.bson.BsonDocument)153 BsonInt32 (org.bson.BsonInt32)55 BsonArray (org.bson.BsonArray)48 Test (org.junit.Test)36 Document (org.bson.Document)32 BsonValue (org.bson.BsonValue)31 BsonInt64 (org.bson.BsonInt64)28 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)14 BsonDouble (org.bson.BsonDouble)14 MongoClientSettings (com.mongodb.MongoClientSettings)13 BsonBinary (org.bson.BsonBinary)13 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)12 HashMap (java.util.HashMap)12 MongoNamespace (com.mongodb.MongoNamespace)11 List (java.util.List)10 Before (org.junit.Before)10 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)9