Search in sources :

Example 41 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)

Example 42 with BsonValue

use of org.bson.BsonValue 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)

Example 43 with BsonValue

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

the class CommandMonitoringTest method massageActualCommandSucceededEvent.

private CommandSucceededEvent massageActualCommandSucceededEvent(final CommandSucceededEvent actual) {
    BsonDocument response = getWritableCloneOfCommand(actual.getResponse());
    // massage numbers that are the wrong BSON type
    response.put("ok", new BsonDouble(response.getNumber("ok").doubleValue()));
    if (response.containsKey("n")) {
        response.put("n", new BsonInt32(response.getNumber("n").intValue()));
    }
    if (actual.getCommandName().equals("find") || actual.getCommandName().equals("getMore")) {
        if (response.containsKey("cursor")) {
            if (response.getDocument("cursor").containsKey("id") && !response.getDocument("cursor").getInt64("id").equals(new BsonInt64(0))) {
                response.getDocument("cursor").put("id", new BsonInt64(42));
            }
        }
    } else if (actual.getCommandName().equals("killCursors")) {
        response.getArray("cursorsUnknown").set(0, new BsonInt64(42));
    } else if (isWriteCommand(actual.getCommandName())) {
        if (response.containsKey("writeErrors")) {
            for (Iterator<BsonValue> iter = response.getArray("writeErrors").iterator(); iter.hasNext(); ) {
                BsonDocument cur = iter.next().asDocument();
                cur.put("code", new BsonInt32(42));
                cur.put("errmsg", new BsonString(""));
            }
        }
        if (actual.getCommandName().equals("update")) {
            response.remove("nModified");
        }
    }
    return new CommandSucceededEvent(actual.getRequestId(), actual.getConnectionDescription(), actual.getCommandName(), response, actual.getElapsedTime(TimeUnit.NANOSECONDS));
}
Also used : BsonInt64(org.bson.BsonInt64) BsonInt32(org.bson.BsonInt32) CommandSucceededEvent(com.mongodb.event.CommandSucceededEvent) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonDouble(org.bson.BsonDouble) BsonValue(org.bson.BsonValue)

Example 44 with BsonValue

use of org.bson.BsonValue 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 45 with BsonValue

use of org.bson.BsonValue 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())) {
        BsonDocument document = fileToArrange.asDocument();
        if (document.containsKey("delete") && document.containsKey("deletes")) {
            for (BsonValue toDelete : document.getArray("deletes")) {
                BsonDocument query = toDelete.asDocument().getDocument("q");
                int limit = toDelete.asDocument().getInt32("limit").getValue();
                MongoCollection<BsonDocument> collection;
                if (document.getString("delete").getValue().equals("fs.files")) {
                    collection = filesCollection;
                } else {
                    collection = chunksCollection;
                }
                if (limit == 1) {
                    collection.deleteOne(query);
                } else {
                    collection.deleteMany(query);
                }
            }
        } else if (document.containsKey("insert") && document.containsKey("documents")) {
            if (document.getString("insert").getValue().equals("fs.files")) {
                filesCollection.insertMany(processFiles(document.getArray("documents"), new ArrayList<BsonDocument>()));
            } else {
                chunksCollection.insertMany(processChunks(document.getArray("documents"), new ArrayList<BsonDocument>()));
            }
        } else if (document.containsKey("update") && document.containsKey("updates")) {
            MongoCollection<BsonDocument> collection;
            if (document.getString("update").getValue().equals("fs.files")) {
                collection = filesCollection;
            } else {
                collection = chunksCollection;
            }
            for (BsonValue rawUpdate : document.getArray("updates")) {
                BsonDocument query = rawUpdate.asDocument().getDocument("q");
                BsonDocument update = rawUpdate.asDocument().getDocument("u");
                update.put("$set", parseHexDocument(update.getDocument("$set")));
                collection.updateMany(query, update);
            }
        } else {
            throw new IllegalArgumentException("Unsupported arrange: " + document);
        }
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) BsonValue(org.bson.BsonValue)

Aggregations

BsonValue (org.bson.BsonValue)45 BsonDocument (org.bson.BsonDocument)37 ArrayList (java.util.ArrayList)17 BsonArray (org.bson.BsonArray)15 BsonString (org.bson.BsonString)14 BsonInt32 (org.bson.BsonInt32)6 BsonObjectId (org.bson.BsonObjectId)4 Test (org.junit.Test)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 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 CommandSucceededEvent (com.mongodb.event.CommandSucceededEvent)2