Search in sources :

Example 26 with BsonArray

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;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 27 with BsonArray

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))));
    }
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) UpdateRequest(com.mongodb.bulk.UpdateRequest) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Example 28 with BsonArray

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;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray)

Example 29 with BsonArray

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));
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 30 with BsonArray

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"));
                }
            }
        }
    }
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BsonString(org.bson.BsonString) BsonObjectId(org.bson.BsonObjectId) BsonDocument(org.bson.BsonDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) List(java.util.List) GridFSUploadOptions(com.mongodb.client.gridfs.model.GridFSUploadOptions) BsonValue(org.bson.BsonValue)

Aggregations

BsonArray (org.bson.BsonArray)32 BsonDocument (org.bson.BsonDocument)29 BsonString (org.bson.BsonString)17 BsonValue (org.bson.BsonValue)15 ArrayList (java.util.ArrayList)11 BsonInt32 (org.bson.BsonInt32)6 BsonInt64 (org.bson.BsonInt64)6 BsonObjectId (org.bson.BsonObjectId)4 ObjectId (org.bson.types.ObjectId)4 BsonDouble (org.bson.BsonDouble)3 Test (org.junit.Test)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 MongoNamespace (com.mongodb.MongoNamespace)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 ReadPreferenceServerSelector (com.mongodb.selector.ReadPreferenceServerSelector)2 WritableServerSelector (com.mongodb.selector.WritableServerSelector)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 List (java.util.List)2 Document (org.bson.Document)2