Search in sources :

Example 56 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 57 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 58 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)

Example 59 with BsonInt32

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

the class CreateCollectionOperation method getCommand.

private BsonDocument getCommand(final ConnectionDescription description) {
    BsonDocument document = new BsonDocument("create", new BsonString(collectionName));
    document.put("autoIndexId", BsonBoolean.valueOf(autoIndex));
    document.put("capped", BsonBoolean.valueOf(capped));
    if (capped) {
        putIfNotZero(document, "size", sizeInBytes);
        putIfNotZero(document, "max", maxDocuments);
    }
    if (usePowerOf2Sizes != null) {
        document.put("flags", new BsonInt32(1));
    }
    if (storageEngineOptions != null) {
        document.put("storageEngine", storageEngineOptions);
    }
    if (indexOptionDefaults != null) {
        document.put("indexOptionDefaults", indexOptionDefaults);
    }
    if (validator != null) {
        document.put("validator", validator);
    }
    if (validationLevel != null) {
        document.put("validationLevel", new BsonString(validationLevel.getValue()));
    }
    if (validationAction != null) {
        document.put("validationAction", new BsonString(validationAction.getValue()));
    }
    appendWriteConcernToCommand(writeConcern, document, description);
    if (collation != null) {
        document.put("collation", collation.asDocument());
    }
    return document;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 60 with BsonInt32

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

the class CreateIndexesOperation method getIndex.

private BsonDocument getIndex(final IndexRequest request) {
    BsonDocument index = new BsonDocument();
    index.append("key", request.getKeys());
    index.append("name", new BsonString(request.getName() != null ? request.getName() : generateIndexName(request.getKeys())));
    index.append("ns", new BsonString(namespace.getFullName()));
    if (request.isBackground()) {
        index.append("background", BsonBoolean.TRUE);
    }
    if (request.isUnique()) {
        index.append("unique", BsonBoolean.TRUE);
    }
    if (request.isSparse()) {
        index.append("sparse", BsonBoolean.TRUE);
    }
    if (request.getExpireAfter(TimeUnit.SECONDS) != null) {
        index.append("expireAfterSeconds", new BsonInt64(request.getExpireAfter(TimeUnit.SECONDS)));
    }
    if (request.getVersion() != null) {
        index.append("v", new BsonInt32(request.getVersion()));
    }
    if (request.getWeights() != null) {
        index.append("weights", request.getWeights());
    }
    if (request.getDefaultLanguage() != null) {
        index.append("default_language", new BsonString(request.getDefaultLanguage()));
    }
    if (request.getLanguageOverride() != null) {
        index.append("language_override", new BsonString(request.getLanguageOverride()));
    }
    if (request.getTextVersion() != null) {
        index.append("textIndexVersion", new BsonInt32(request.getTextVersion()));
    }
    if (request.getSphereVersion() != null) {
        index.append("2dsphereIndexVersion", new BsonInt32(request.getSphereVersion()));
    }
    if (request.getBits() != null) {
        index.append("bits", new BsonInt32(request.getBits()));
    }
    if (request.getMin() != null) {
        index.append("min", new BsonDouble(request.getMin()));
    }
    if (request.getMax() != null) {
        index.append("max", new BsonDouble(request.getMax()));
    }
    if (request.getBucketSize() != null) {
        index.append("bucketSize", new BsonDouble(request.getBucketSize()));
    }
    if (request.getDropDups()) {
        index.append("dropDups", BsonBoolean.TRUE);
    }
    if (request.getStorageEngine() != null) {
        index.append("storageEngine", request.getStorageEngine());
    }
    if (request.getPartialFilterExpression() != null) {
        index.append("partialFilterExpression", request.getPartialFilterExpression());
    }
    if (request.getCollation() != null) {
        index.append("collation", request.getCollation().asDocument());
    }
    return index;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonDouble(org.bson.BsonDouble)

Aggregations

BsonInt32 (org.bson.BsonInt32)106 BsonDocument (org.bson.BsonDocument)91 BsonString (org.bson.BsonString)58 BsonArray (org.bson.BsonArray)26 Test (org.junit.Test)26 Document (org.bson.Document)23 BsonInt64 (org.bson.BsonInt64)20 Test (org.junit.jupiter.api.Test)16 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)13 List (java.util.List)9 BsonDouble (org.bson.BsonDouble)9 DisplayName (org.junit.jupiter.api.DisplayName)8 BsonNull (org.bson.BsonNull)7 MongoClientSettings (com.mongodb.MongoClientSettings)6 TestCommandListener (com.mongodb.internal.connection.TestCommandListener)5 Map (java.util.Map)5 BsonBoolean (org.bson.BsonBoolean)5 ClusterFixture.serverVersionAtLeast (com.mongodb.ClusterFixture.serverVersionAtLeast)4 ServerAddress (com.mongodb.ServerAddress)4