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, final WriteConcern writeConcern, final DBEncoder encoder) 

Source Link

Document

Remove documents from a collection.

Usage

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public void delete(Object entity, Object pKey) {
    EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata,
            entity.getClass());/*from ww  w . ja  v a  2s  .c  o  m*/
    MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata()
            .getMetamodel(entityMetadata.getPersistenceUnit());
    AbstractManagedType managedType = (AbstractManagedType) metaModel.entity(entityMetadata.getEntityClazz());

    DBObject query = new BasicDBObject();

    if (managedType.hasLobAttribute()) {
        KunderaGridFS gfs = new KunderaGridFS(mongoDb, entityMetadata.getTableName());
        String id = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName();
        query.put("metadata." + id, pKey);
        gfs.remove(query);
    }

    else {
        if (metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) {
            MongoDBUtils.populateCompoundKey(query, entityMetadata, metaModel, pKey);
        } else {
            query.put("_id", MongoDBUtils.populateValue(pKey, pKey.getClass()));
        }
        // For secondary tables.
        List<String> secondaryTables = ((DefaultEntityAnnotationProcessor) managedType.getEntityAnnotation())
                .getSecondaryTablesName();
        secondaryTables.add(entityMetadata.getTableName());

        for (String collectionName : secondaryTables) {
            KunderaCoreUtils.printQuery("Drop existing collection:" + query, showQuery);
            DBCollection dbCollection = mongoDb.getCollection(collectionName);
            dbCollection.remove(query, getWriteConcern(), encoder);
        }

        getIndexManager().remove(entityMetadata, entity, pKey);
    }
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

public void deleteByColumn(String schemaName, String tableName, String columnName, Object columnValue) {
    DBCollection dbCollection = mongoDb.getCollection(tableName);
    BasicDBObject query = new BasicDBObject();
    query.put(columnName, columnValue);/*w ww.  j  a  va  2s  . c o m*/
    KunderaCoreUtils.printQuery("Delete column:" + query, showQuery);
    dbCollection.remove(query, getWriteConcern(), encoder);
}

From source file:ezbake.data.mongo.MongoDriverHandler.java

License:Apache License

public EzWriteResult remove_driver(String collection, EzRemoveRequest req, EzSecurityToken token)
        throws EzMongoDriverException, TException {
    //        if (!collection.equals("fs.chunks") && !collection.equals("fs.files")) {
    collection = appName + "_" + collection;
    //        }/*from  w ww.  j  a  v  a2  s . co  m*/

    appLog.debug("remove_driver() to collection: {}", collection);

    TokenUtils.validateSecurityToken(token, handler.getConfigurationProperties());
    EzWriteResult ewr = new EzWriteResult();
    try {
        DBCollection c = handler.db.getCollection(normalizeCollection(collection));

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbObjectQuery()));
        DBObject query = (DBObject) ois.readObject();

        ois = new ObjectInputStream(new ByteArrayInputStream(req.getWriteConcern()));
        WriteConcern writeConcern = (WriteConcern) ois.readObject();

        ois = new ObjectInputStream(new ByteArrayInputStream(req.getDbEncoder()));
        DBEncoder dbEncoder = (DBEncoder) ois.readObject();

        HashMap<String, String> auditParamsMap = new HashMap<>();
        auditParamsMap.put("action", "remove_driver");
        auditParamsMap.put("collectionName", collection);
        auditParamsMap.put("query", handler.printMongoObject(query));
        auditParamsMap.put("writeConcern", handler.printMongoObject(writeConcern));
        auditParamsMap.put("dbEncoder", handler.printMongoObject(dbEncoder));
        handler.auditLog(token, AuditEventType.FileObjectDelete, auditParamsMap);
        WriteResult res = c.remove(query, writeConcern, dbEncoder);

        appLog.debug("remove() WriteResult: {}", res);

        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        new ObjectOutputStream(bOut).writeObject(res);

        ewr.setWriteResult(bOut.toByteArray());
    } catch (Exception e) {
        appLog.error(e.toString());
        EzMongoDriverException eme = new EzMongoDriverException();
        eme.setEx(ser(e));
        throw eme;
    }
    return ewr;
}