Search in sources :

Example 31 with BsonValue

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;
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) HashMap(java.util.HashMap) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 32 with BsonValue

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

Example 33 with BsonValue

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));
}
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 34 with BsonValue

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

Example 35 with BsonValue

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);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Aggregations

BsonValue (org.bson.BsonValue)47 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)15 BsonInt32 (org.bson.BsonInt32)6 Test (org.junit.Test)5 BsonObjectId (org.bson.BsonObjectId)4 MongoNamespace (com.mongodb.MongoNamespace)3 AggregateToCollectionOperation (com.mongodb.operation.AggregateToCollectionOperation)3 List (java.util.List)3 BsonInt64 (org.bson.BsonInt64)3 BsonNumber (org.bson.BsonNumber)3 Document (org.bson.Document)3 ObjectId (org.bson.types.ObjectId)3 Before (org.junit.Before)3 ConnectionString (com.mongodb.ConnectionString)2 MongoGridFSException (com.mongodb.MongoGridFSException)2 GridFSUploadOptions (com.mongodb.client.gridfs.model.GridFSUploadOptions)2 FindOptions (com.mongodb.client.model.FindOptions)2