Search in sources :

Example 16 with BsonDocument

use of org.bson.BsonDocument 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 17 with BsonDocument

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

the class GridFSTest method processFiles.

private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
Also used : BsonInt64(org.bson.BsonInt64) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 18 with BsonDocument

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

the class QueryProtocol method sendQuerySucceededEvent.

private void sendQuerySucceededEvent(final ConnectionDescription connectionDescription, final long startTimeNanos, final QueryMessage message, final boolean isExplainEvent, final ResponseBuffers responseBuffers, final QueryResult<T> queryResult) {
    if (commandListener != null) {
        BsonDocument response = asFindCommandResponseDocument(responseBuffers, queryResult, isExplainEvent);
        sendCommandSucceededEvent(message, getCommandName(isExplainEvent), response, connectionDescription, startTimeNanos, commandListener);
    }
}
Also used : BsonDocument(org.bson.BsonDocument)

Example 19 with BsonDocument

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

the class Projections method combine.

private static Bson combine(final List<String> fieldNames, final BsonValue value) {
    BsonDocument document = new BsonDocument();
    for (String fieldName : fieldNames) {
        document.remove(fieldName);
        document.append(fieldName, value);
    }
    return document;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString)

Example 20 with BsonDocument

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

the class SimpleExpression method toBsonDocument.

@Override
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName(name);
    BuildersHelper.encodeValue(writer, expression, codecRegistry);
    writer.writeEndDocument();
    return writer.getDocument();
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Aggregations

BsonDocument (org.bson.BsonDocument)166 BsonString (org.bson.BsonString)53 BsonValue (org.bson.BsonValue)37 Test (org.junit.Test)34 BsonArray (org.bson.BsonArray)29 BsonInt32 (org.bson.BsonInt32)28 ArrayList (java.util.ArrayList)24 BsonDocumentReader (org.bson.BsonDocumentReader)17 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)14 BsonDocumentWriter (org.bson.BsonDocumentWriter)14 BsonInt64 (org.bson.BsonInt64)14 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)10 BsonDouble (org.bson.BsonDouble)8 MongoNamespace (com.mongodb.MongoNamespace)6 Before (org.junit.Before)6 MongoGridFSException (com.mongodb.MongoGridFSException)5 BsonObjectId (org.bson.BsonObjectId)5 BsonWriter (org.bson.BsonWriter)5 Document (org.bson.Document)5 ObjectId (org.bson.types.ObjectId)5