use of com.mongodb.client.result.DeleteResult 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);
}
}
}
use of com.mongodb.client.result.DeleteResult in project mongo-java-driver by mongodb.
the class GridFSBucketImpl method delete.
@Override
public void delete(final BsonValue id) {
DeleteResult result = filesCollection.deleteOne(new BsonDocument("_id", id));
chunksCollection.deleteMany(new BsonDocument("files_id", id));
if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
throw new MongoGridFSException(format("No file found with the id: %s", id));
}
}
use of com.mongodb.client.result.DeleteResult in project camel by apache.
the class MongoDbIdempotentRepository method remove.
@ManagedOperation(description = "Remove the key from the store")
@Override
public boolean remove(E key) {
Document document = new Document("_id", key);
DeleteResult res = collection.deleteOne(document);
return res.getDeletedCount() > 0;
}
use of com.mongodb.client.result.DeleteResult in project camel by apache.
the class MongoDbOperationsTest method testRemove.
@Test
public void testRemove() throws Exception {
// Prepare test
assertEquals(0, testCollection.count());
for (int i = 1; i <= 100; i++) {
String body = null;
Formatter f = new Formatter();
if (i % 2 == 0) {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
} else {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
}
f.close();
template.requestBody("direct:insert", body);
}
assertEquals(100L, testCollection.count());
// Testing the update logic
BasicDBObject extraField = new BasicDBObject("extraField", true);
assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
Exchange resultExchange = template.request("direct:remove", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(extraField);
}
});
Object result = resultExchange.getOut().getBody();
assertTrue(result instanceof DeleteResult);
assertEquals("Number of records deleted header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0, testCollection.count(extraField));
}
use of com.mongodb.client.result.DeleteResult in project camel by apache.
the class MongoDbOperationsTest method testRemove.
@Test
public void testRemove() throws Exception {
// Prepare test
assertEquals(0, testCollection.count());
for (int i = 1; i <= 100; i++) {
String body = null;
try (Formatter f = new Formatter()) {
if (i % 2 == 0) {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString();
} else {
body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i).toString();
}
f.close();
}
template.requestBody("direct:insert", body);
}
assertEquals(100L, testCollection.count());
// Testing the update logic
Document extraField = new Document("extraField", true);
assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField));
Exchange resultExchange = template.request("direct:remove", new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(extraField);
}
});
Object result = resultExchange.getOut().getBody();
assertTrue(result instanceof DeleteResult);
assertEquals("Number of records deleted header should equal 50", 50L, resultExchange.getOut().getHeader(MongoDbConstants.RECORDS_AFFECTED));
assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0, testCollection.count(extraField));
}
Aggregations