Search in sources :

Example 21 with BsonInt32

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

the class AggregateOperation 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 (!isInline(description)) {
        BsonDocument cursor = new BsonDocument();
        if (batchSize != null) {
            cursor.put("batchSize", new BsonInt32(batchSize));
        }
        commandDocument.put("cursor", cursor);
    }
    if (allowDiskUse != null) {
        commandDocument.put("allowDiskUse", BsonBoolean.valueOf(allowDiskUse));
    }
    if (!readConcern.isServerDefault()) {
        commandDocument.put("readConcern", readConcern.asDocument());
    }
    if (collation != null) {
        commandDocument.put("collation", collation.asDocument());
    }
    return commandDocument;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 22 with BsonInt32

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

the class AsyncQueryBatchCursor method asGetMoreCommandDocument.

private BsonDocument asGetMoreCommandDocument(final long cursorId) {
    BsonDocument document = new BsonDocument("getMore", new BsonInt64(cursorId)).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 23 with BsonInt32

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

the class BaseWriteOperation method manufactureGetLastErrorResponse.

private BsonDocument manufactureGetLastErrorResponse(final MongoBulkWriteException e) {
    BsonDocument response = new BsonDocument();
    addBulkWriteResultToResponse(e.getWriteResult(), response);
    if (e.getWriteConcernError() != null) {
        response.putAll(e.getWriteConcernError().getDetails());
    }
    if (getLastError(e) != null) {
        response.put("err", new BsonString(getLastError(e).getMessage()));
        response.put("code", new BsonInt32(getLastError(e).getCode()));
        response.putAll(getLastError(e).getDetails());
    } else if (e.getWriteConcernError() != null) {
        response.put("err", new BsonString(e.getWriteConcernError().getMessage()));
        response.put("code", new BsonInt32(e.getWriteConcernError().getCode()));
    }
    return response;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 24 with BsonInt32

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

the class BaseWriteOperation method addBulkWriteResultToResponse.

private void addBulkWriteResultToResponse(final BulkWriteResult bulkWriteResult, final BsonDocument response) {
    response.put("ok", new BsonInt32(1));
    if (getType() == INSERT) {
        response.put("n", new BsonInt32(0));
    } else if (getType() == DELETE) {
        response.put("n", new BsonInt32(bulkWriteResult.getDeletedCount()));
    } else if (getType() == UPDATE || getType() == REPLACE) {
        response.put("n", new BsonInt32(bulkWriteResult.getMatchedCount() + bulkWriteResult.getUpserts().size()));
        if (bulkWriteResult.getUpserts().isEmpty()) {
            response.put("updatedExisting", BsonBoolean.TRUE);
        } else {
            response.put("updatedExisting", BsonBoolean.FALSE);
            response.put("upserted", bulkWriteResult.getUpserts().get(0).getId());
        }
    }
}
Also used : BsonInt32(org.bson.BsonInt32)

Example 25 with BsonInt32

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

the class UpdateProtocol method appendToWriteCommandResponseDocument.

@Override
protected void appendToWriteCommandResponseDocument(final RequestMessage curMessage, final RequestMessage nextMessage, final WriteConcernResult writeConcernResult, final BsonDocument response) {
    response.append("n", new BsonInt32(writeConcernResult.getCount()));
    UpdateMessage updateMessage = (UpdateMessage) curMessage;
    UpdateRequest updateRequest = updateMessage.getUpdateRequests().get(0);
    BsonValue upsertedId = null;
    if (writeConcernResult.getUpsertedId() != null) {
        upsertedId = writeConcernResult.getUpsertedId();
    } else if (!writeConcernResult.isUpdateOfExisting() && updateRequest.isUpsert()) {
        if (updateRequest.getUpdate().containsKey("_id")) {
            upsertedId = updateRequest.getUpdate().get("_id");
        } else if (updateRequest.getFilter().containsKey("_id")) {
            upsertedId = updateRequest.getFilter().get("_id");
        }
    }
    if (upsertedId != null) {
        response.append("upserted", new BsonArray(singletonList(new BsonDocument("index", new BsonInt32(0)).append("_id", upsertedId))));
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) UpdateRequest(com.mongodb.bulk.UpdateRequest) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Aggregations

BsonInt32 (org.bson.BsonInt32)32 BsonDocument (org.bson.BsonDocument)28 BsonString (org.bson.BsonString)18 Test (org.junit.Test)8 BsonArray (org.bson.BsonArray)6 BsonInt64 (org.bson.BsonInt64)6 BsonValue (org.bson.BsonValue)6 Document (org.bson.Document)3 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)3 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)3 HashMap (java.util.HashMap)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 BsonDouble (org.bson.BsonDouble)2 BsonNumber (org.bson.BsonNumber)2 ServerAddress (com.mongodb.ServerAddress)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 UpdateRequest (com.mongodb.bulk.UpdateRequest)1 UpdateResult (com.mongodb.client.result.UpdateResult)1 DescriptionHelper.createServerDescription (com.mongodb.connection.DescriptionHelper.createServerDescription)1 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)1