Search in sources :

Example 1 with BsonArray

use of org.bson.BsonArray in project mongo-java-driver by mongodb.

the class GetMoreProtocol method asGetMoreCommandResponseDocument.

private BsonDocument asGetMoreCommandResponseDocument(final QueryResult<T> queryResult, final ResponseBuffers responseBuffers) {
    List<ByteBufBsonDocument> rawResultDocuments = Collections.emptyList();
    if (responseBuffers.getReplyHeader().getNumberReturned() != 0) {
        responseBuffers.getBodyByteBuffer().position(0);
        rawResultDocuments = ByteBufBsonDocument.create(responseBuffers);
    }
    BsonDocument cursorDocument = new BsonDocument("id", queryResult.getCursor() == null ? new BsonInt64(0) : new BsonInt64(queryResult.getCursor().getId())).append("ns", new BsonString(namespace.getFullName())).append("nextBatch", new BsonArray(rawResultDocuments));
    return new BsonDocument("cursor", cursorDocument).append("ok", new BsonDouble(1));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonDouble(org.bson.BsonDouble)

Example 2 with BsonArray

use of org.bson.BsonArray in project mongo-java-driver by mongodb.

the class CrudTest method getInsertManyMongoOperation.

private MongoOperationInsertManyResult getInsertManyMongoOperation(final BsonDocument arguments) {
    return new MongoOperationInsertManyResult() {

        @Override
        public void execute() {
            final List<BsonDocument> documents = new ArrayList<BsonDocument>();
            for (BsonValue document : arguments.getArray("documents")) {
                documents.add(document.asDocument());
            }
            collection.insertMany(documents, new SingleResultCallback<Void>() {

                @Override
                public void onResult(final Void result, final Throwable t) {
                    if (t != null) {
                        getCallback().onResult(null, t);
                    } else {
                        BsonArray insertedIds = new BsonArray();
                        for (BsonDocument document : documents) {
                            insertedIds.add(document.get("_id"));
                        }
                        getCallback().onResult(new InsertManyResult(insertedIds), null);
                    }
                }
            });
        }
    };
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BsonValue(org.bson.BsonValue)

Example 3 with BsonArray

use of org.bson.BsonArray in project mongo-java-driver by mongodb.

the class GridFSTest method arrangeGridFS.

private void arrangeGridFS(final BsonDocument arrange) {
    if (arrange.isEmpty()) {
        return;
    }
    for (BsonValue fileToArrange : arrange.getArray("data", new BsonArray())) {
        final BsonDocument document = fileToArrange.asDocument();
        if (document.containsKey("delete") && document.containsKey("deletes")) {
            for (BsonValue toDelete : document.getArray("deletes")) {
                final BsonDocument query = toDelete.asDocument().getDocument("q");
                int limit = toDelete.asDocument().getInt32("limit").getValue();
                final MongoCollection<BsonDocument> collection;
                if (document.getString("delete").getValue().equals("fs.files")) {
                    collection = filesCollection;
                } else {
                    collection = chunksCollection;
                }
                if (limit == 1) {
                    new MongoOperation<DeleteResult>() {

                        @Override
                        public void execute() {
                            collection.deleteOne(query, getCallback());
                        }
                    }.get();
                } else {
                    new MongoOperation<DeleteResult>() {

                        @Override
                        public void execute() {
                            collection.deleteMany(query, getCallback());
                        }
                    }.get();
                }
            }
        } else if (document.containsKey("insert") && document.containsKey("documents")) {
            if (document.getString("insert").getValue().equals("fs.files")) {
                new MongoOperation<Void>() {

                    @Override
                    public void execute() {
                        filesCollection.insertMany(processFiles(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
                    }
                }.get();
            } else {
                new MongoOperation<Void>() {

                    @Override
                    public void execute() {
                        chunksCollection.insertMany(processChunks(document.getArray("documents"), new ArrayList<BsonDocument>()), getCallback());
                    }
                }.get();
            }
        } else if (document.containsKey("update") && document.containsKey("updates")) {
            final MongoCollection<BsonDocument> collection;
            if (document.getString("update").getValue().equals("fs.files")) {
                collection = filesCollection;
            } else {
                collection = chunksCollection;
            }
            for (BsonValue rawUpdate : document.getArray("updates")) {
                final BsonDocument query = rawUpdate.asDocument().getDocument("q");
                final BsonDocument update = rawUpdate.asDocument().getDocument("u");
                update.put("$set", parseHexDocument(update.getDocument("$set")));
                new MongoOperation<UpdateResult>() {

                    @Override
                    public void execute() {
                        collection.updateMany(query, update, getCallback());
                    }
                }.get();
            }
        } else {
            throw new IllegalArgumentException("Unsupported arrange: " + document);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) DeleteResult(com.mongodb.client.result.DeleteResult) UpdateResult(com.mongodb.client.result.UpdateResult) BsonValue(org.bson.BsonValue)

Example 4 with BsonArray

use of org.bson.BsonArray in project mongo-java-driver by mongodb.

the class ListCollectionsOperation method asQueryDocument.

private BsonDocument asQueryDocument(final ConnectionDescription connectionDescription, final ReadPreference readPreference) {
    BsonDocument document = new BsonDocument();
    BsonDocument transformedFilter = null;
    if (filter != null) {
        if (filter.containsKey("name")) {
            if (!filter.isString("name")) {
                throw new IllegalArgumentException("When filtering collections on MongoDB versions < 3.0 the name field " + "must be a string");
            }
            transformedFilter = new BsonDocument();
            transformedFilter.putAll(filter);
            transformedFilter.put("name", new BsonString(format("%s.%s", databaseName, filter.getString("name").getValue())));
        } else {
            transformedFilter = filter;
        }
    }
    BsonDocument indexExcludingRegex = new BsonDocument("name", new BsonRegularExpression("^[^$]*$"));
    BsonDocument query = transformedFilter == null ? indexExcludingRegex : new BsonDocument("$and", new BsonArray(asList(indexExcludingRegex, transformedFilter)));
    document.put("$query", query);
    if (connectionDescription.getServerType() == SHARD_ROUTER && !readPreference.equals(primary())) {
        document.put("$readPreference", readPreference.toDocument());
    }
    if (maxTimeMS > 0) {
        document.put("$maxTimeMS", new BsonInt64(maxTimeMS));
    }
    return document;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) BsonRegularExpression(org.bson.BsonRegularExpression)

Example 5 with BsonArray

use of org.bson.BsonArray in project mongo-java-driver by mongodb.

the class UpdateProtocol method getAsWriteCommand.

@Override
protected BsonDocument getAsWriteCommand(final ByteBufferBsonOutput bsonOutput, final int firstDocumentPosition) {
    List<ByteBufBsonDocument> documents = ByteBufBsonDocument.create(bsonOutput, firstDocumentPosition);
    BsonDocument updateDocument = new BsonDocument("q", documents.get(0)).append("u", documents.get(1));
    if (updates.get(0).isMulti()) {
        updateDocument.append("multi", BsonBoolean.TRUE);
    }
    if (updates.get(0).isUpsert()) {
        updateDocument.append("upsert", BsonBoolean.TRUE);
    }
    return getBaseCommandDocument("update").append("updates", new BsonArray(singletonList(updateDocument)));
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray)

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