Search in sources :

Example 6 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())) {
        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 7 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 8 with BsonValue

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

the class IndexHelper method generateIndexName.

/**
     * Convenience method to generate an index name from the set of fields it is over.
     *
     * @return a string representation of this index's fields
     */
static String generateIndexName(final BsonDocument index) {
    StringBuilder indexName = new StringBuilder();
    for (final String keyNames : index.keySet()) {
        if (indexName.length() != 0) {
            indexName.append('_');
        }
        indexName.append(keyNames).append('_');
        BsonValue ascOrDescValue = index.get(keyNames);
        if (ascOrDescValue instanceof BsonNumber) {
            indexName.append(((BsonNumber) ascOrDescValue).intValue());
        } else if (ascOrDescValue instanceof BsonString) {
            indexName.append(((BsonString) ascOrDescValue).getValue().replace(' ', '_'));
        }
    }
    return indexName.toString();
}
Also used : BsonNumber(org.bson.BsonNumber) BsonString(org.bson.BsonString) BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue)

Example 9 with BsonValue

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

the class ConnectionStringTest method assertExpectedHosts.

private void assertExpectedHosts(final List<String> hosts) {
    List<String> cleanedHosts = new ArrayList<String>();
    for (String host : hosts) {
        if (host.startsWith("[")) {
            int idx = host.indexOf("]");
            cleanedHosts.add(host.substring(1, idx) + host.substring(idx + 1));
        } else {
            cleanedHosts.add(host);
        }
    }
    List<String> expectedHosts = new ArrayList<String>();
    for (BsonValue rawHost : definition.getArray("hosts")) {
        BsonDocument hostDoc = rawHost.asDocument();
        String host = hostDoc.getString("host").getValue();
        String port = "";
        if (!hostDoc.get("port").isNull()) {
            port = ":" + hostDoc.getInt32("port").getValue();
        }
        expectedHosts.add(host + port);
    }
    assertEquals(expectedHosts, cleanedHosts);
}
Also used : BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) ConnectionString(com.mongodb.ConnectionString) BsonValue(org.bson.BsonValue)

Example 10 with BsonValue

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

the class WriteCommandResultHelper method getUpsertedItems.

@SuppressWarnings("unchecked")
private static List<BulkWriteUpsert> getUpsertedItems(final BsonDocument result) {
    BsonValue upsertedValue = result.get("upserted");
    if (upsertedValue == null) {
        return Collections.emptyList();
    } else {
        List<BulkWriteUpsert> bulkWriteUpsertList = new ArrayList<BulkWriteUpsert>();
        for (BsonValue upsertedItem : (BsonArray) upsertedValue) {
            BsonDocument upsertedItemDocument = (BsonDocument) upsertedItem;
            bulkWriteUpsertList.add(new BulkWriteUpsert(upsertedItemDocument.getNumber("index").intValue(), upsertedItemDocument.get("_id")));
        }
        return bulkWriteUpsertList;
    }
}
Also used : BulkWriteUpsert(com.mongodb.bulk.BulkWriteUpsert) BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) 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