Search in sources :

Example 21 with DeleteResult

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());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) UpdateStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.UpdateStrategy) BasicDBObject(com.mongodb.BasicDBObject) Document(org.bson.Document) QueryStrategy(com.creditease.uav.datastore.mongo.MongodbImplStrategy.QueryStrategy) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DeleteResult(com.mongodb.client.result.DeleteResult) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 22 with DeleteResult

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;
    }
}
Also used : Document(org.bson.Document) DeleteResult(com.mongodb.client.result.DeleteResult) DBException(com.yahoo.ycsb.DBException)

Example 23 with DeleteResult

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);
                        }
                    }
                });
            }
        }
    });
}
Also used : BsonDocument(org.bson.BsonDocument) MongoGridFSException(com.mongodb.MongoGridFSException) SingleResultCallback(com.mongodb.async.SingleResultCallback) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 24 with DeleteResult

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);
        }
    });
}
Also used : Document(org.bson.Document) DeleteResult(com.mongodb.client.result.DeleteResult)

Example 25 with DeleteResult

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;
}
Also used : DeleteResult(com.mongodb.client.result.DeleteResult) Bson(org.bson.conversions.Bson) ManagedOperation(org.apache.camel.api.management.ManagedOperation)

Aggregations

DeleteResult (com.mongodb.client.result.DeleteResult)46 Document (org.bson.Document)20 UpdateResult (com.mongodb.client.result.UpdateResult)10 BsonDocument (org.bson.BsonDocument)9 Test (org.junit.jupiter.api.Test)9 BasicDBObject (com.mongodb.BasicDBObject)7 WriteConcern (com.mongodb.WriteConcern)6 Bson (org.bson.conversions.Bson)6 MongoException (com.mongodb.MongoException)5 ArrayList (java.util.ArrayList)5 DataAccessException (org.springframework.dao.DataAccessException)5 MongoGridFSException (com.mongodb.MongoGridFSException)4 MongoClient (com.mongodb.client.MongoClient)4 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)4 ReadPreference (com.mongodb.ReadPreference)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 InsertOneResult (com.mongodb.client.result.InsertOneResult)3 MongoClient (com.mongodb.reactivestreams.client.MongoClient)3 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)3 ObjectId (org.bson.types.ObjectId)3