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;
}
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());
}
}
}
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))));
}
}
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;
}
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;
}
Aggregations