use of org.bson.BsonArray 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;
}
use of org.bson.BsonArray 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.BsonArray in project mongo-java-driver by mongodb.
the class CreateViewOperation method getCommand.
private BsonDocument getCommand(final ConnectionDescription description) {
BsonDocument commandDocument = new BsonDocument("create", new BsonString(viewName)).append("viewOn", new BsonString(viewOn)).append("pipeline", new BsonArray(pipeline));
if (collation != null) {
commandDocument.put("collation", collation.asDocument());
}
appendWriteConcernToCommand(writeConcern, commandDocument, description);
return commandDocument;
}
use of org.bson.BsonArray 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.BsonArray in project mongo-java-driver by mongodb.
the class GridFSTest method doUpload.
private void doUpload(final BsonDocument rawArguments, final BsonDocument assertion) {
Throwable error = null;
ObjectId objectId = null;
BsonDocument arguments = parseHexDocument(rawArguments, "source");
try {
String filename = arguments.getString("filename").getValue();
InputStream input = new ByteArrayInputStream(arguments.getBinary("source").getData());
GridFSUploadOptions options = new GridFSUploadOptions();
BsonDocument rawOptions = arguments.getDocument("options", new BsonDocument());
if (rawOptions.containsKey("chunkSizeBytes")) {
options = options.chunkSizeBytes(rawOptions.getInt32("chunkSizeBytes").getValue());
}
if (rawOptions.containsKey("metadata")) {
options = options.metadata(Document.parse(rawOptions.getDocument("metadata").toJson()));
}
objectId = gridFSBucket.uploadFromStream(filename, input, options);
} catch (Throwable e) {
error = e;
}
if (assertion.containsKey("error")) {
// We don't need to read anything more so don't see the extra chunk
if (!assertion.getString("error").getValue().equals("ExtraChunk")) {
assertNotNull("Should have thrown an exception", error);
}
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data", new BsonArray())) {
BsonDocument dataItem = rawDataItem.asDocument();
String insert = dataItem.getString("insert", new BsonString("none")).getValue();
if (insert.equals("expected.files")) {
List<BsonDocument> documents = processFiles(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
assertEquals(filesCollection.count(), documents.size());
BsonDocument actual = filesCollection.find().first();
for (BsonDocument expected : documents) {
assertEquals(expected.get("length"), actual.get("length"));
assertEquals(expected.get("chunkSize"), actual.get("chunkSize"));
assertEquals(expected.get("md5"), actual.get("md5"));
assertEquals(expected.get("filename"), actual.get("filename"));
if (expected.containsKey("metadata")) {
assertEquals(expected.get("metadata"), actual.get("metadata"));
}
}
} else if (insert.equals("expected.chunks")) {
List<BsonDocument> documents = processChunks(dataItem.getArray("documents", new BsonArray()), new ArrayList<BsonDocument>());
assertEquals(chunksCollection.count(), documents.size());
List<BsonDocument> actualDocuments = chunksCollection.find().into(new ArrayList<BsonDocument>());
for (int i = 0; i < documents.size(); i++) {
BsonDocument expected = documents.get(i);
BsonDocument actual = actualDocuments.get(i);
assertEquals(new BsonObjectId(objectId), actual.getObjectId("files_id"));
assertEquals(expected.get("n"), actual.get("n"));
assertEquals(expected.get("data"), actual.get("data"));
}
}
}
}
}
Aggregations