Search in sources :

Example 11 with BsonString

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

the class WriteProtocol method getResponseDocument.

private BsonDocument getResponseDocument(final RequestMessage curMessage, final RequestMessage nextMessage, final WriteConcernResult writeConcernResult, final WriteConcernException writeConcernException) {
    BsonDocument response = new BsonDocument("ok", new BsonInt32(1));
    if (writeConcern.isAcknowledged()) {
        if (writeConcernException == null) {
            appendToWriteCommandResponseDocument(curMessage, nextMessage, writeConcernResult, response);
        } else {
            response.put("n", new BsonInt32(0));
            BsonDocument writeErrorDocument = new BsonDocument("index", new BsonInt32(0)).append("code", new BsonInt32(writeConcernException.getErrorCode()));
            if (writeConcernException.getErrorMessage() != null) {
                writeErrorDocument.append("errmsg", new BsonString(writeConcernException.getErrorMessage()));
            }
            response.put("writeErrors", new BsonArray(singletonList(writeErrorDocument)));
        }
    }
    return response;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 12 with BsonString

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

the class AggregateExplainOperation method getCommand.

private BsonDocument getCommand() {
    BsonDocument commandDocument = new BsonDocument("aggregate", new BsonString(namespace.getCollectionName()));
    commandDocument.put("pipeline", new BsonArray(pipeline));
    commandDocument.put("explain", BsonBoolean.TRUE);
    if (maxTimeMS > 0) {
        commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS));
    }
    if (allowDiskUse != null) {
        commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse));
    }
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 13 with BsonString

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

the class AggregateToCollectionOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument commandDocument = new BsonDocument("aggregate", new BsonString(namespace.getCollectionName()));
    commandDocument.put("pipeline", new BsonArray(pipeline));
    if (maxTimeMS > 0) {
        commandDocument.put("maxTimeMS", new BsonInt64(maxTimeMS));
    }
    if (allowDiskUse != null) {
        commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse));
    }
    if (bypassDocumentValidation != null && serverIsAtLeastVersionThreeDotTwo(description)) {
        commandDocument.put("bypassDocumentValidation", BsonBoolean.valueOf(bypassDocumentValidation));
    }
    if (serverIsAtLeastVersionThreeDotSix(description)) {
        commandDocument.put("cursor", new BsonDocument());
    }
    appendWriteConcernToCommand(writeConcern, commandDocument, description);
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 14 with BsonString

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

the class CreateIndexesOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument command = new BsonDocument("createIndexes", new BsonString(namespace.getCollectionName()));
    List<BsonDocument> values = new ArrayList<BsonDocument>();
    for (IndexRequest request : requests) {
        values.add(getIndex(request));
    }
    command.put("indexes", new BsonArray(values));
    appendWriteConcernToCommand(writeConcern, command, description);
    return command;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) IndexRequest(com.mongodb.bulk.IndexRequest)

Example 15 with BsonString

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

the class GridFSTest method doDelete.

private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
    Throwable error = null;
    try {
        gridFSBucket.delete(arguments.getObjectId("id").getValue());
    } catch (MongoGridFSException e) {
        error = e;
    }
    if (assertion.containsKey("error")) {
        assertNotNull("Should have thrown an exception", error);
    } else {
        assertNull("Should not have thrown an exception", error);
        for (BsonValue rawDataItem : assertion.getArray("data")) {
            BsonDocument dataItem = rawDataItem.asDocument();
            for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
                String delete = dataItem.getString("delete", new BsonString("none")).getValue();
                BsonObjectId id = new BsonObjectId(new ObjectId());
                if (delete.equals("expected.files")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
                } else if (delete.equals("expected.chunks")) {
                    id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
                }
                assertEquals(filesCollection.count(new BsonDocument("_id", id)), 0);
                assertEquals(chunksCollection.count(new BsonDocument("files_id", id)), 0);
            }
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonString(org.bson.BsonString) MongoGridFSException(com.mongodb.MongoGridFSException) BsonArray(org.bson.BsonArray) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonValue(org.bson.BsonValue)

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