Search in sources :

Example 31 with BsonArray

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

the class DBObjectCodecTest method shouldEncodedNestedMapsListsAndDocuments.

@Test
public void shouldEncodedNestedMapsListsAndDocuments() {
    //  {"0" : 0, "1", 1}
    byte[] zeroOneDocumentBytes = new byte[] { 19, 0, 0, 0, 16, 48, 0, 0, 0, 0, 0, 16, 49, 0, 1, 0, 0, 0, 0 };
    Map<String, Object> zeroOneMap = new HashMap<String, Object>();
    zeroOneMap.put("0", 0);
    zeroOneMap.put("1", 1);
    DBObject zeroOneDBObject = new BasicDBObject();
    zeroOneDBObject.putAll(zeroOneMap);
    DBObject zeroOneDBList = new BasicDBList();
    zeroOneDBList.putAll(zeroOneMap);
    List<Integer> zeroOneList = asList(0, 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject().append("map", zeroOneMap).append("dbDocument", zeroOneDBObject).append("dbList", zeroOneDBList).append("list", zeroOneList).append("array", new int[] { 0, 1 }).append("lazyDoc", new LazyDBObject(zeroOneDocumentBytes, new LazyBSONCallback())).append("lazyArray", new LazyDBList(zeroOneDocumentBytes, new LazyBSONCallback()));
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonDocument zeroOneBsonDocument = new BsonDocument().append("0", new BsonInt32(0)).append("1", new BsonInt32(1));
    BsonArray zeroOneBsonArray = new BsonArray(asList(new BsonInt32(0), new BsonInt32(1)));
    assertEquals(new BsonDocument("map", zeroOneBsonDocument).append("dbDocument", zeroOneBsonDocument).append("dbList", zeroOneBsonArray).append("list", zeroOneBsonArray).append("array", zeroOneBsonArray).append("lazyDoc", zeroOneBsonDocument).append("lazyArray", zeroOneBsonArray), writer.getDocument());
}
Also used : HashMap(java.util.HashMap) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) LazyBSONCallback(org.bson.LazyBSONCallback) BsonArray(org.bson.BsonArray) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 32 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())) {
        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

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