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());
}
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);
}
}
}
Aggregations