Example usage for com.mongodb DBCollection remove

List of usage examples for com.mongodb DBCollection remove

Introduction

In this page you can find the example usage for com.mongodb DBCollection remove.

Prototype

public WriteResult remove(final DBObject query) 

Source Link

Document

Remove documents from a collection.

Usage

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@Override
public <T extends Document> int remove(Collection<T> collection, Map<String, Map<Key, Condition>> toRemove) {
    log("remove", toRemove);
    int num = 0;//  w  w w.  j  a  v a  2  s .  com
    DBCollection dbCollection = getDBCollection(collection);
    long start = PERFLOG.start();
    try {
        List<String> batchIds = Lists.newArrayList();
        List<DBObject> batch = Lists.newArrayList();
        Iterator<Entry<String, Map<Key, Condition>>> it = toRemove.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Map<Key, Condition>> entry = it.next();
            QueryBuilder query = createQueryForUpdate(entry.getKey(), entry.getValue());
            batchIds.add(entry.getKey());
            batch.add(query.get());
            if (!it.hasNext() || batch.size() == IN_CLAUSE_BATCH_SIZE) {
                DBObject q = new BasicDBObject();
                q.put(QueryOperators.OR, batch);
                try {
                    num += dbCollection.remove(q).getN();
                } catch (Exception e) {
                    throw DocumentStoreException.convert(e, "Remove failed for " + batch);
                } finally {
                    if (collection == Collection.NODES) {
                        invalidateCache(batchIds);
                    }
                }
                batchIds.clear();
                batch.clear();
            }
        }
    } finally {
        PERFLOG.end(start, 1, "remove keys={}", toRemove);
    }
    return num;
}

From source file:org.apache.karaf.jaas.modules.mongo.internal.DefaultUserDetailService.java

License:Apache License

@Override
public void deleteUser(String username) throws Exception {

    DBCollection users = getDB().getCollection(configuration.getUserCollectionName());

    DBObject userQuery = new BasicDBObject("username", username);
    users.remove(userQuery);

    // / FIXME also remove from all role definitions

}

From source file:org.aw20.mongoworkbench.command.CollectionRemoveAllMongoCommand.java

License:Open Source License

@Override
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);
    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);/* w w  w .  ja v  a 2s . com*/

    DBCollection collection = db.getCollection(sColl);
    collection.remove(new BasicDBObject());

    setMessage("count=" + collection.count());
}

From source file:org.bananaforscale.cormac.dao.document.DocumentDataServiceImpl.java

License:Apache License

/**
 * Removes all documents in the collection. Not the most efficient approach
 * but if you have a collection that was created with certain options and
 * want to clear everything out this will preserve the configuration. As the
 * new Java API requires you to iterate and delete its more efficient to use
 * the old API./*from   w ww.j a  v a  2  s .  c om*/
 *
 * @param databaseName the database
 * @param collectionName the collection
 * @return the result of the operation
 * @throws DatasourceException
 * @throws NotFoundException
 */
@Override
public boolean deleteAll(String databaseName, String collectionName)
        throws DatasourceException, NotFoundException {
    try {

        if (!databaseExists(databaseName)) {
            throw new NotFoundException("The database doesn't exist in the datasource");
        }
        if (!collectionExists(databaseName, collectionName)) {
            throw new NotFoundException("The collection doesn't exist in the datasource");
        }
        DB mongoDatabase = mongoClient.getDB(databaseName);
        DBCollection collection = mongoDatabase.getCollection(collectionName);
        collection.remove(new BasicDBObject());
        return true;
    } catch (MongoException ex) {
        logger.error("An error occured while updating the document", ex);
        throw new DatasourceException("An error occured while updating the document");
    }
}

From source file:org.benjp.services.mongodb.ChatServiceImpl.java

License:Open Source License

private void ensureIndexInRoom(String room) {
    DBCollection coll = db().getCollection(M_ROOM_PREFIX + room);
    BasicDBObject doc = new BasicDBObject();
    doc.put("timestamp", System.currentTimeMillis());
    coll.insert(doc);//from  w  w w.  j  a v a2 s.c  o  m
    ConnectionManager.getInstance().ensureIndexesInRoom(room);
    coll.remove(doc);
}

From source file:org.benjp.services.mongodb.NotificationServiceImpl.java

License:Open Source License

public static void cleanupNotifications() {
    DBCollection coll = ConnectionManager.getInstance().getDB().getCollection(M_NOTIFICATIONS);
    BasicDBObject query = new BasicDBObject();
    query.put("timestamp", new BasicDBObject("$lt", System.currentTimeMillis() - 24 * 60 * 60 * 1000));
    //    query.put("isRead", true);
    DBCursor cursor = coll.find(query);// w ww  .  j  ava  2 s.  co  m
    while (cursor.hasNext()) {
        DBObject doc = cursor.next();
        coll.remove(doc);
    }
}

From source file:org.benjp.services.mongodb.NotificationServiceImpl.java

License:Open Source License

public void setNotificationsAsRead(String user, String type, String category, String categoryId) {
    DBCollection coll = db().getCollection(M_NOTIFICATIONS);
    BasicDBObject query = new BasicDBObject();
    query.put("user", user);
    if (categoryId != null)
        query.put("categoryId", categoryId);
    if (category != null)
        query.put("category", category);
    if (type != null)
        query.put("type", type);
    //    query.put("isRead", false);
    coll.remove(query);
    //    DBCursor cursor = coll.find(query);
    //    while (cursor.hasNext())
    //    {//from  w w  w . j  a  va2 s. c  o m
    //      DBObject doc = cursor.next();
    //      doc.put("isRead", true);
    //      coll.save(doc, WriteConcern.SAFE);
    //    }

}

From source file:org.broad.igv.plugin.mongocollab.MongoCollabPlugin.java

License:Open Source License

public static void removeFeature(DBCollection collection, DBFeature featDBObject) {
    collection.remove(featDBObject);
}

From source file:org.canedata.provider.mongodb.command.Truncate.java

License:Apache License

public <D> D execute(EntityFactory factory, Resource<?> res, Entity target, Object... args) {
    // specified collection name.
    // or use default collection.
    MongoResource mres = (MongoResource) res;

    DB db = null;/* ww w  . j a  v a2  s. c o  m*/
    String coll_name = target.getName();

    if (args != null) {
        // take db from resource.
        if (args.length >= 2) {
            db = mres.take(args[0]);
            coll_name = (String) args[1];
        } else {
            db = mres.take();

            if (args.length >= 1)
                coll_name = (String) args[0];
        }

    }

    DBCollection coll = db.getCollection(coll_name);

    WriteResult rlt = coll.remove(new BasicDBObject());
    return (D) rlt;
}

From source file:org.chililog.server.data.Controller.java

License:Apache License

/**
 * Removes the specified business object form mongoDB
 * /*  ww w.  j  av  a 2  s  .  com*/
 * @param db
 *            MongoDb connection
 * @param businessObject
 *            business object to remove from the database
 * @throws ChiliLogException
 *             if there is any error during deleting
 */
public void remove(DB db, BO businessObject) throws ChiliLogException {
    if (db == null) {
        throw new NullArgumentException("db");
    }
    if (businessObject == null) {
        throw new NullArgumentException("businessObject");
    }

    try {
        DBCollection coll = db.getCollection(this.getDBCollectionName());
        if (businessObject.isExistingRecord()) {
            DBObject obj = new BasicDBObject();
            obj.put(BO.DOCUMENT_ID_FIELD_NAME, businessObject.getDocumentID());
            coll.remove(obj);
        }
    } catch (MongoException ex) {
        throw new ChiliLogException(ex, Strings.MONGODB_REMOVE_ERROR, ex.getMessage());
    }
}