Example usage for com.mongodb.client MongoCollection findOneAndDelete

List of usage examples for com.mongodb.client MongoCollection findOneAndDelete

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection findOneAndDelete.

Prototype

@Nullable
TDocument findOneAndDelete(Bson filter);

Source Link

Document

Atomically find a document and remove it.

Usage

From source file:com.centurylink.mdw.mongo.MongoDocumentDb.java

License:Apache License

@Override
public boolean deleteDocument(String ownerType, Long documentId) {
    MongoCollection<org.bson.Document> collection = getMongoDb().getCollection(ownerType);
    return collection.findOneAndDelete(eq("document_id", documentId)) != null;
}

From source file:com.github.cherimojava.data.mongo.entity.EntityInvocationHandler.java

License:Apache License

/**
 * removes the given EntityInvocationHandler represented Entity from the given Collection
 *
 * @param handler//from  w w w . jav  a 2  s .  co m
 *            EntityInvocationHandler (Entity) to drop
 * @param coll
 *            MongoCollection in which this entity is saved
 */
static <T extends Entity> void drop(EntityInvocationHandler handler, MongoCollection<T> coll) {
    coll.findOneAndDelete(new Document(ID, (handler.proxy).get(ID)));
}

From source file:com.shampan.model.VideoModel.java

/**
 * This method will delete a video//from   www .  j a  v  a2s .  co m
 *
 * @param videoId video Id
 * @author created by Rashida on 21 October
 */
public String deleteVideo(String videoId) {
    MongoCollection<VideoDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.VIDEOS.toString(), VideoDAO.class);
    BasicDBObject selectQuery = (BasicDBObject) QueryBuilder.start("videoId").is(videoId).get();
    VideoDAO result = mongoCollection.findOneAndDelete(selectQuery);
    if (result != null) {
        resultEvent.setResponseCode(PropertyProvider.get("Deleted"));
        return resultEvent.toString();
    } else {
        resultEvent.setResponseCode(PropertyProvider.get("BadRequest"));
        return resultEvent.toString();
    }
}

From source file:org.iotivity.cloud.rdserver.MongoDB.java

License:Open Source License

/**
 * API for deleting resources about a particular device ID in collection
 *
 * @param di//from w  ww . j a  v a  2s  .  c  om
 *            device id
 * @param tablename
 *            collection name
 */
public void deleteResourceAboutDid(String di, String tablename) {

    MongoCollection<Document> collection = db.getCollection(tablename);

    collection.findOneAndDelete(Filters.eq(Constants.RS_DEVICE_ID, di));
}

From source file:org.iotivity.cloud.rdserver.MongoDB.java

License:Open Source License

/**
 * API for deleting resources about a particular device ID and ins in
 * collection/*from  www . j a va2  s  .co  m*/
 *
 * @param di
 *            device id
 * @param ins
 *            ins
 * @param tablename
 *            collection name
 */
public void deleteResourceAboutDidAndIns(String di, String ins, String tablename) {

    MongoCollection<Document> collection = db.getCollection(tablename);

    collection.findOneAndDelete(
            Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di), Filters.eq(Constants.RS_INS, ins)));

}

From source file:org.restheart.db.DAOUtils.java

License:Open Source License

/**
 *
 * @param coll/* w  w w  . j  a v  a2 s  .c o m*/
 * @param documentId use Optional.empty() to specify no documentId (null is
 * _id: null)
 * @param shardKeys
 * @param data
 * @param replace
 * @param returnNew
 * @return the new or old document depending on returnNew
 */
public static OperationResult updateDocument(MongoCollection<BsonDocument> coll, Object documentId,
        BsonDocument shardKeys, BsonDocument data, boolean replace, boolean returnNew) {
    Objects.requireNonNull(coll);
    Objects.requireNonNull(data);

    BsonDocument document = getUpdateDocument(data);

    Bson query;

    boolean idPresent = true;

    if (documentId instanceof Optional && !((Optional) documentId).isPresent()) {
        query = IMPOSSIBLE_CONDITION;
        idPresent = false;
    } else {
        query = eq("_id", documentId);
    }

    if (shardKeys != null) {
        query = and(query, shardKeys);
    }

    if (replace) {
        // here we cannot use the atomic findOneAndReplace because it does
        // not support update operators.

        BsonDocument oldDocument;

        if (idPresent) {
            oldDocument = coll.findOneAndDelete(query);
        } else {
            oldDocument = null;
        }

        BsonDocument newDocument = coll.findOneAndUpdate(query, document, FAU_AFTER_UPSERT_OPS);

        return new OperationResult(-1, oldDocument, newDocument);
    } else if (returnNew) {
        BsonDocument newDocument = coll.findOneAndUpdate(query, document, FAU_AFTER_UPSERT_OPS);

        return new OperationResult(-1, null, newDocument);
    } else {
        BsonDocument oldDocument = coll.findOneAndUpdate(query, document, FAU_UPSERT_OPS);

        return new OperationResult(-1, oldDocument, null);
    }
}

From source file:org.restheart.db.DocumentDAO.java

License:Open Source License

/**
 * @param dbName//  www  .  j a va 2 s . c o  m
 * @param collName
 * @param documentId
 * @param shardedKeys
 * @param requestEtag
 * @param checkEtag
 * @return
 */
@Override
public OperationResult deleteDocument(final String dbName, final String collName, final Object documentId,
        final BsonDocument shardedKeys, final String requestEtag, final boolean checkEtag) {
    MongoDatabase mdb = client.getDatabase(dbName);
    MongoCollection<BsonDocument> mcoll = mdb.getCollection(collName, BsonDocument.class);

    BsonDocument oldDocument = mcoll.findOneAndDelete(getIdFilter(documentId, shardedKeys));

    if (oldDocument == null) {
        return new OperationResult(HttpStatus.SC_NOT_FOUND);
    } else if (checkEtag) {
        // check the old etag (in case restore the old document version)
        return optimisticCheckEtag(mcoll, null, oldDocument, null, requestEtag, HttpStatus.SC_NO_CONTENT, true);
    } else {
        return new OperationResult(HttpStatus.SC_NO_CONTENT);
    }
}

From source file:rapture.repo.mongodb.MongoDbDataStore.java

License:Open Source License

@Override
public boolean delete(String key) {
    MongoCollection<Document> collection = getCollection();
    Document query = new Document(KEY, key);
    boolean deleted = null != collection.findOneAndDelete(query);
    if (deleted && needsFolderHandling) {
        dirRepo.dropFileEntry(key);//from w  w w.  j  a v a2  s . c o  m
    }
    return deleted;
}

From source file:rapture.table.mongodb.MongoIndexHandler.java

License:Open Source License

@Override
public void removeAll(String rowId) {
    String key = getKey(rowId);// w w w.  j av  a  2 s .c  om
    MongoCollection<Document> collection = MongoDBFactory.getCollection(instanceName, tableName);
    Document query = new Document();
    query.put(KEY, key);
    collection.findOneAndDelete(query);
}