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.mobicents.servlet.sip.restcomm.dao.mongodb.MongoAccountsDao.java

License:Open Source License

private void removeAccount(final DBCollection collection, final Sid sid) {
    final BasicDBObject query = new BasicDBObject();
    query.put("sid", sid.toString());
    final WriteResult result = collection.remove(query);
    if (!result.getLastError().ok()) {
        logger.error(result.getLastError().getErrorMessage());
    }//from  w ww  .ja  va 2 s  .  c  o m
}

From source file:org.mongoj.samples.service.persistence.CarPersistenceImpl.java

License:Open Source License

protected void removeImpl(String id) throws UpdateException, SystemException {
    DBObject criteria = new QueryBuilder().put("_id").is(id).get();

    DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME);

    WriteResult writeResult = collection.remove(criteria);

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from ww w .jav  a 2 s  .  com
}

From source file:org.mongoj.samples.service.persistence.CarPersistenceImpl.java

License:Open Source License

protected Car removeImpl(Car car) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(CarImpl.COLLECTION_NAME);

    WriteResult writeResult = collection.remove(getDBObject(car));

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from   w ww.  j  a v  a 2  s . c  om

    return car;
}

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

protected void removeImpl(String id) throws UpdateException, SystemException {
    DBObject criteria = new QueryBuilder().put("_id").is(id).get();

    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    WriteResult writeResult = collection.remove(criteria);

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from  ww  w  . jav a 2s . c  o  m
}

From source file:org.mongoj.samples.service.persistence.UserPersistenceImpl.java

License:Open Source License

protected User removeImpl(User user) throws UpdateException, SystemException {
    DBCollection collection = getDB().getCollection(UserImpl.COLLECTION_NAME);

    WriteResult writeResult = collection.remove(getDBObject(user));

    String err = writeResult.getError();

    if (err != null) {
        throw new UpdateException(err);
    }//from  w w w  .ja  v  a 2s .c  o m

    return user;
}

From source file:org.mule.module.mongo.tools.MongoRestoreDirectory.java

License:Open Source License

private void restore() throws IOException {
    Validate.notNull(inputPath);//from   w ww.j a v  a 2  s. c  o  m
    List<RestoreFile> restoreFiles = getRestoreFiles(inputPath);
    List<RestoreFile> oplogRestores = new ArrayList<RestoreFile>();
    for (RestoreFile restoreFile : restoreFiles) {
        if (!isOplog(restoreFile.getCollection())) {
            if (drop && !BackupUtils.isSystemCollection(restoreFile.getCollection())) {
                mongoClient.dropCollection(restoreFile.getCollection());
            }

            DBCollection dbCollection = mongoClient.getCollection(restoreFile.getCollection());
            List<DBObject> dbObjects = restoreFile.getCollectionObjects();

            if (BackupUtils.isUserCollection(restoreFile.getCollection())) {
                for (DBObject currentUser : dbCollection.find()) {
                    if (!dbObjects.contains(currentUser)) {
                        dbCollection.remove(currentUser);
                    }
                }
            }

            for (DBObject dbObject : dbObjects) {
                dbCollection.save(dbObject);
            }
        } else {
            oplogRestores.add(restoreFile);
        }
    }
    if (oplogReplay && !oplogRestores.isEmpty()) {
        for (RestoreFile oplogRestore : oplogRestores) {
            mongoClient.executeComamnd(
                    new BasicDBObject("applyOps", filterOplogForDatabase(oplogRestore).toArray()));
        }
    }
}

From source file:org.ndsc.mimicIO.db.DBManager.java

License:Apache License

/**
 * Removes document from db.//from   www. j  a v a2s . c  o m
 *
 * @param obj the object to remove
 */
public void removeDoc(Persistable obj) {
    // Suppress error stream when MongoDB raises java.net.ConnectException
    // in another component (and cannot be caught)
    PrintStream ps = System.err;
    System.setErr(null);
    try {
        DBCollection collection = this.collections.get(obj.getDBName());
        collection.remove(new BasicDBObject(obj.getDBObject()));
    } catch (Exception e) {
        log.error("Failed to remove document from database: {}", e.getMessage());
    } finally {
        // Restore error stream
        System.setErr(ps);
    }
}

From source file:org.netbeans.modules.mongodb.ui.windows.collectionview.actions.DeleteSelectedDocumentAction.java

License:Open Source License

@Override
public void actionPerformed(ActionEvent e) {
    final DBObject document = getView().getResultTableSelectedDocument();
    final Object dlgResult = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(
            Bundle.confirmDocumentDeletionText(), NotifyDescriptor.YES_NO_OPTION));
    if (dlgResult.equals(NotifyDescriptor.OK_OPTION)) {
        try {//from www .  j av  a  2  s  . c  o  m
            final DBCollection dbCollection = getView().getLookup().lookup(DBCollection.class);
            dbCollection.remove(document);
            getView().refreshResults();
        } catch (MongoException ex) {
            DialogDisplayer.getDefault().notify(
                    new NotifyDescriptor.Message(ex.getLocalizedMessage(), NotifyDescriptor.ERROR_MESSAGE));
        }
    }
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepositoryTestCase.java

License:Open Source License

protected void clearMongoDb() throws UnknownHostException {
    MongoClient mongoClient = MongoDBRepository.newMongoClient(descriptor);
    try {/*from w  w  w. j ava 2  s .  c o m*/
        DBCollection coll = MongoDBRepository.getCollection(descriptor, mongoClient);
        coll.dropIndexes();
        coll.remove(new BasicDBObject());
        coll = MongoDBRepository.getCountersCollection(descriptor, mongoClient);
        coll.dropIndexes();
        coll.remove(new BasicDBObject());
    } finally {
        mongoClient.close();
    }
}

From source file:org.nuxeo.ecm.core.test.StorageConfiguration.java

License:Apache License

protected void clearMongoDB(MongoDBRepositoryDescriptor descriptor) throws UnknownHostException {
    MongoClient mongoClient = MongoDBRepository.newMongoClient(descriptor);
    try {//  ww w.jav a 2  s  .  c  om
        DBCollection coll = MongoDBRepository.getCollection(descriptor, mongoClient);
        coll.dropIndexes();
        coll.remove(new BasicDBObject());
        coll = MongoDBRepository.getCountersCollection(descriptor, mongoClient);
        coll.dropIndexes();
        coll.remove(new BasicDBObject());
    } finally {
        mongoClient.close();
    }
}