use of org.bson.BsonValue in project mongo-java-driver by mongodb.
the class QueryProtocol method asFindCommandDocument.
private BsonDocument asFindCommandDocument(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) {
BsonDocument command = new BsonDocument(FIND_COMMAND_NAME, new BsonString(namespace.getCollectionName()));
boolean isExplain = false;
List<ByteBufBsonDocument> documents = ByteBufBsonDocument.create(bsonOutput, firstDocumentPosition);
ByteBufBsonDocument rawQueryDocument = documents.get(0);
for (Map.Entry<String, BsonValue> cur : rawQueryDocument.entrySet()) {
String commandFieldName = META_OPERATOR_TO_COMMAND_FIELD_MAP.get(cur.getKey());
if (commandFieldName != null) {
command.append(commandFieldName, cur.getValue());
} else if (cur.getKey().equals("$explain")) {
isExplain = true;
}
}
if (command.size() == 1) {
command.append("filter", rawQueryDocument);
}
if (documents.size() == 2) {
command.append("projection", documents.get(1));
}
if (skip != 0) {
command.append("skip", new BsonInt32(skip));
}
if (withLimitAndBatchSize) {
if (limit != 0) {
command.append("limit", new BsonInt32(limit));
}
if (batchSize != 0) {
command.append("batchSize", new BsonInt32(batchSize));
}
}
if (tailableCursor) {
command.append("tailable", BsonBoolean.valueOf(tailableCursor));
}
if (noCursorTimeout) {
command.append("noCursorTimeout", BsonBoolean.valueOf(noCursorTimeout));
}
if (oplogReplay) {
command.append("oplogReplay", BsonBoolean.valueOf(oplogReplay));
}
if (awaitData) {
command.append("awaitData", BsonBoolean.valueOf(awaitData));
}
if (partial) {
command.append("allowPartialResults", BsonBoolean.valueOf(partial));
}
if (isExplain) {
command = new BsonDocument(EXPLAIN_COMMAND_NAME, command);
}
return command;
}
use of org.bson.BsonValue 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.BsonValue in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getInsertManyResult.
BsonDocument getInsertManyResult(final BsonDocument arguments) {
List<BsonDocument> documents = new ArrayList<BsonDocument>();
for (BsonValue document : arguments.getArray("documents")) {
documents.add(document.asDocument());
}
collection.insertMany(documents, new InsertManyOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue()));
BsonArray insertedIds = new BsonArray();
for (BsonDocument document : documents) {
insertedIds.add(document.get("_id"));
}
return toResult(new BsonDocument("insertedIds", insertedIds));
}
use of org.bson.BsonValue in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getBulkWriteResult.
BsonDocument getBulkWriteResult(final BsonDocument arguments) {
WriteConcern writeConcern = WriteConcern.ACKNOWLEDGED;
if (arguments.containsKey("writeConcern")) {
if (arguments.getDocument("writeConcern").size() > 1) {
throw new UnsupportedOperationException("Write concern document contains unexpected keys: " + arguments.getDocument("writeConcern").keySet());
}
writeConcern = new WriteConcern(arguments.getDocument("writeConcern").getInt32("w").intValue());
}
List<WriteModel<BsonDocument>> writeModels = new ArrayList<WriteModel<BsonDocument>>();
for (Iterator<BsonValue> iter = arguments.getArray("requests").iterator(); iter.hasNext(); ) {
BsonDocument cur = iter.next().asDocument();
if (cur.get("insertOne") != null) {
writeModels.add(new InsertOneModel<BsonDocument>(cur.getDocument("insertOne").getDocument("document")));
} else if (cur.get("updateOne") != null) {
writeModels.add(new UpdateOneModel<BsonDocument>(cur.getDocument("updateOne").getDocument("filter"), cur.getDocument("updateOne").getDocument("update")));
} else {
throw new UnsupportedOperationException("Unsupported write request type");
}
}
return toResult(collection.withWriteConcern(writeConcern).bulkWrite(writeModels, new BulkWriteOptions().ordered(arguments.getBoolean("ordered", BsonBoolean.TRUE).getValue())));
}
use of org.bson.BsonValue in project mongo-java-driver by mongodb.
the class JsonPoweredCrudTestHelper method getAggregateResult.
BsonDocument getAggregateResult(final BsonDocument arguments) {
if (!serverVersionAtLeast(2, 6)) {
Assume.assumeFalse(description.contains("$out"));
}
List<BsonDocument> pipeline = new ArrayList<BsonDocument>();
for (BsonValue stage : arguments.getArray("pipeline")) {
pipeline.add(stage.asDocument());
}
AggregateIterable<BsonDocument> iterable = collection.aggregate(pipeline);
if (arguments.containsKey("batchSize")) {
iterable.batchSize(arguments.getNumber("batchSize").intValue());
}
if (arguments.containsKey("collation")) {
iterable.collation(getCollation(arguments.getDocument("collation")));
}
return toResult(iterable);
}
Aggregations