use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class CrudTest method assertCollectionEquals.
private void assertCollectionEquals(final BsonDocument expectedCollection) {
BsonArray actual = new MongoOperation<BsonArray>() {
@Override
public void execute() {
MongoCollection<BsonDocument> collectionToCompare = collection;
if (expectedCollection.containsKey("name")) {
collectionToCompare = getDefaultDatabase().getCollection(expectedCollection.getString("name").getValue(), BsonDocument.class);
}
collectionToCompare.find().into(new BsonArray(), getCallback());
}
}.get();
assertEquals(description, expectedCollection.getArray("data"), actual);
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class GridFSTest method setUp.
@Before
@Override
public void setUp() {
super.setUp();
gridFSBucket = GridFSBuckets.create(database);
filesCollection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), "fs.files")).withDocumentClass(BsonDocument.class);
chunksCollection = Fixture.initializeCollection(new MongoNamespace(getDefaultDatabaseName(), "fs.chunks")).withDocumentClass(BsonDocument.class);
final List<BsonDocument> filesDocuments = processFiles(data.getArray("files", new BsonArray()), new ArrayList<BsonDocument>());
if (!filesDocuments.isEmpty()) {
new MongoOperation<Void>() {
@Override
public void execute() {
filesCollection.insertMany(filesDocuments, getCallback());
}
}.get();
}
final List<BsonDocument> chunksDocuments = processChunks(data.getArray("chunks", new BsonArray()), new ArrayList<BsonDocument>());
if (!chunksDocuments.isEmpty()) {
new MongoOperation<Void>() {
@Override
public void execute() {
chunksCollection.insertMany(chunksDocuments, getCallback());
}
}.get();
}
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class GridFSTest method doDelete.
private void doDelete(final BsonDocument arguments, final BsonDocument assertion) {
Throwable error = null;
try {
new MongoOperation<Void>() {
@Override
public void execute() {
gridFSBucket.delete(arguments.getObjectId("id").getValue(), getCallback());
}
}.get();
} catch (MongoGridFSException e) {
error = e;
}
if (assertion.containsKey("error")) {
assertNotNull("Should have thrown an exception", error);
} else {
assertNull("Should not have thrown an exception", error);
for (BsonValue rawDataItem : assertion.getArray("data")) {
BsonDocument dataItem = rawDataItem.asDocument();
for (BsonValue deletedItem : dataItem.getArray("deletes", new BsonArray())) {
String delete = dataItem.getString("delete", new BsonString("none")).getValue();
BsonObjectId id = new BsonObjectId(new ObjectId());
if (delete.equals("expected.files")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("_id");
} else if (delete.equals("expected.chunks")) {
id = deletedItem.asDocument().getDocument("q").getObjectId("files_id");
}
long filesCount = getFilesCount(new BsonDocument("_id", id));
long chunksCount = getChunksCount(new BsonDocument("files_id", id));
assertEquals(filesCount, 0);
assertEquals(chunksCount, 0);
}
}
}
}
use of org.bson.BsonArray 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 {
final String filename = arguments.getString("filename").getValue();
final InputStream inputStream = new ByteArrayInputStream(arguments.getBinary("source").getData());
final GridFSUploadOptions options = new GridFSUploadOptions();
BsonDocument rawOptions = arguments.getDocument("options", new BsonDocument());
if (rawOptions.containsKey("chunkSizeBytes")) {
options.chunkSizeBytes(rawOptions.getInt32("chunkSizeBytes").getValue());
}
if (rawOptions.containsKey("metadata")) {
options.metadata(Document.parse(rawOptions.getDocument("metadata").toJson()));
}
objectId = new MongoOperation<ObjectId>() {
@Override
public void execute() {
gridFSBucket.uploadFromStream(filename, toAsyncInputStream(inputStream), options, getCallback());
}
}.get();
} 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(getFilesCount(new BsonDocument()), documents.size());
BsonDocument actual = new MongoOperation<BsonDocument>() {
@Override
public void execute() {
filesCollection.find().first(getCallback());
}
}.get();
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(getChunksCount(new BsonDocument()), documents.size());
List<BsonDocument> actualDocuments = new MongoOperation<List<BsonDocument>>() {
@Override
public void execute() {
chunksCollection.find().into(new ArrayList<BsonDocument>(), getCallback());
}
}.get();
for (int i = 0; i < documents.size(); i++) {
BsonDocument expected = documents.get(i);
BsonDocument actual;
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"));
}
}
}
}
}
use of org.bson.BsonArray in project mongo-java-driver by mongodb.
the class Filters method geoWithinPolygon.
/**
* Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified
* polygon.
*
* @param fieldName the field name
* @param points a list of pairs of x, y coordinates. Any extra dimensions are ignored
* @return the filter
* @mongodb.driver.manual reference/operator/query/geoWithin/ $geoWithin
* @mongodb.driver.manual reference/operator/query/polygon/#op._S_polygon $polygon
* @mongodb.server.release 2.4
* @since 3.1
*/
public static Bson geoWithinPolygon(final String fieldName, final List<List<Double>> points) {
BsonArray pointsArray = new BsonArray();
for (List<Double> point : points) {
pointsArray.add(new BsonArray(asList(new BsonDouble(point.get(0)), new BsonDouble(point.get(1)))));
}
BsonDocument polygon = new BsonDocument("$polygon", pointsArray);
return new OperatorFilter<BsonDocument>("$geoWithin", fieldName, polygon);
}
Aggregations