Search in sources :

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

Example 47 with BsonValue

use of org.bson.BsonValue in project spring-data-mongodb by spring-projects.

the class ReactiveFindOperationSupportTests method distinctReturnsRawValuesIfReturnTypeIsBsonValue.

// DATAMONGO-1761
@Test
public void distinctReturnsRawValuesIfReturnTypeIsBsonValue() {
    Consumer<BsonValue> inValues = in(new BsonString("solo"), new BsonString("skywalker"));
    StepVerifier.create(template.query(Person.class).distinct("lastname").as(BsonValue.class).all()).assertNext(// 
    inValues).assertNext(// 
    inValues).verifyComplete();
}
Also used : BsonString(org.bson.BsonString) BsonValue(org.bson.BsonValue) Test(org.junit.Test)

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