Search in sources :

Example 6 with BsonArray

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

the class WriteCommandResultHelper method getUpsertedItems.

@SuppressWarnings("unchecked")
private static List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) {
    BsonValue upsertedValue = result.get("upserted");
    if (upsertedValue == null) {
        return Collections.emptyList();
    } else {
        List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>();
        for (BsonValue upsertedItem : (BsonArray) upsertedValue) {
            BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem;
            bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id")));
        }
        return bulkWriteUpsertList;
    }
}
Also used : BulkWriteUpsert(com.mongodb.bulk.BulkWriteUpsert) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Example 7 with BsonArray

use of org.bson.BsonArray 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 8 with BsonArray

use of org.bson.BsonArray 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 9 with BsonArray

use of org.bson.BsonArray 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 10 with BsonArray

use of org.bson.BsonArray 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)

Aggregations

BsonArray (org.bson.BsonArray)32 BsonDocument (org.bson.BsonDocument)29 BsonString (org.bson.BsonString)17 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)11 BsonInt32 (org.bson.BsonInt32)6 BsonInt64 (org.bson.BsonInt64)6 BsonObjectId (org.bson.BsonObjectId)4 ObjectId (org.bson.types.ObjectId)4 BsonDouble (org.bson.BsonDouble)3 Test (org.junit.Test)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 MongoNamespace (com.mongodb.MongoNamespace)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 ReadPreferenceServerSelector (com.mongodb.selector.ReadPreferenceServerSelector)2 WritableServerSelector (com.mongodb.selector.WritableServerSelector)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 List (java.util.List)2 Document (org.bson.Document)2