use of com.mongodb.client.result.DeleteResult in project uavstack by uavorg.
the class MongoDBDataStore method update.
@SuppressWarnings("rawtypes")
@Override
protected boolean update(DataStoreMsg msg) {
boolean isSuccess = false;
String collectionName = (String) msg.get(DataStoreProtocol.MONGO_COLLECTION_NAME);
MongoCollection<Document> collection = this.datasource.getSourceConnect().getCollection(collectionName);
// collection no exist
if (null == collection) {
if (log.isTraceEnable()) {
log.warn(this, "MongoDB[" + this.datasource.getDataStoreConnection().getDbName() + "] Collection[" + collectionName + "] NO EXIST.");
}
return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
// prepare query
String updateObj = (String) adaptor.prepareUpdateObj(msg, this.datasource.getDataStoreConnection());
// @SuppressWarnings("rawtypes")
Map params = JSONHelper.toObject(DataStoreHelper.decorateInForMongoDB(updateObj), Map.class);
BasicDBObject condition = new BasicDBObject();
BasicDBObject update = new BasicDBObject();
boolean isRemove = true;
for (Object keyObj : params.keySet()) {
if (keyObj.toString().equals(DataStoreProtocol.WHERE)) {
QueryStrategy qry = new QueryStrategy();
qry.concretProcessor(DataStoreProtocol.WHERE, params, condition);
} else if (keyObj.toString().equals(DataStoreProtocol.UPDATE)) {
isRemove = false;
UpdateStrategy up = new UpdateStrategy();
up.concretProcessor(DataStoreProtocol.UPDATE, params, update);
} else {
log.err(this, "can not figure out, please check it out " + keyObj.toString());
}
}
if (isRemove) {
log.info(this, "condition: " + condition.toString());
DeleteResult res = collection.deleteMany(condition);
log.info(this, "DeletedCount:" + res.getDeletedCount());
isSuccess = true;
} else {
log.info(this, "condition: " + condition.toString());
log.info(this, "update: " + update.toString());
UpdateResult res = collection.updateMany(condition, update);
log.info(this, "ModifiedCount:" + res.getModifiedCount());
isSuccess = true;
}
return adaptor.handleUpdateResult(isSuccess, msg, this.datasource.getDataStoreConnection());
}
use of com.mongodb.client.result.DeleteResult in project YCSB by brianfrankcooper.
the class MongoDbClient method delete.
/**
* Delete a record from the database.
*
* @param table
* The name of the table
* @param key
* The record key of the record to delete.
* @return Zero on success, a non-zero error code on error. See the {@link DB}
* class's description for a discussion of error codes.
*/
@Override
public Status delete(String table, String key) {
try {
MongoCollection<Document> collection = database.getCollection(table);
Document query = new Document("_id", key);
DeleteResult result = collection.withWriteConcern(writeConcern).deleteOne(query);
if (result.wasAcknowledged() && result.getDeletedCount() == 0) {
System.err.println("Nothing deleted for key " + key);
return Status.NOT_FOUND;
}
return Status.OK;
} catch (Exception e) {
System.err.println(e.toString());
return Status.ERROR;
}
}
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, final SingleResultCallback<Void> callback) {
notNull("id", id);
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
filesCollection.deleteOne(new BsonDocument("_id", id), new SingleResultCallback<DeleteResult>() {
@Override
public void onResult(final DeleteResult filesResult, final Throwable t) {
if (t != null) {
errHandlingCallback.onResult(null, t);
} else {
chunksCollection.deleteMany(new BsonDocument("files_id", id), new SingleResultCallback<DeleteResult>() {
@Override
public void onResult(final DeleteResult chunksResult, final Throwable t) {
if (t != null) {
errHandlingCallback.onResult(null, t);
} else if (filesResult.wasAcknowledged() && filesResult.getDeletedCount() == 0) {
errHandlingCallback.onResult(null, new MongoGridFSException(format("No file found with the ObjectId: %s", id)));
} else {
errHandlingCallback.onResult(null, null);
}
}
});
}
}
});
}
use of com.mongodb.client.result.DeleteResult in project mongo-java-driver by mongodb.
the class GridFSUploadStreamImpl method abort.
@Override
public void abort(final SingleResultCallback<Void> callback) {
notNull("callback", callback);
final SingleResultCallback<Void> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
if (!takeWritingLock(errHandlingCallback)) {
return;
}
chunksCollection.deleteMany(new Document("files_id", fileId), new SingleResultCallback<DeleteResult>() {
@Override
public void onResult(final DeleteResult result, final Throwable t) {
releaseWritingLock();
errHandlingCallback.onResult(null, t);
}
});
}
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) {
Bson document = eq(MONGO_ID, key);
DeleteResult res = collection.deleteOne(document);
return res.getDeletedCount() > 0;
}
Aggregations