Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public <T extends Model> DBCursor findRaw(Class<T> clazz, Filter filter) {

    DBObject query = this.getCachedFilter(filter);

    DBCollection dbCollection = this.getCollection(clazz);

    if (dbCollection == null) {
        LOG.warn("No collection found for clazz [{}]", clazz.getName());
        return null;
    }//from w w w.j av  a 2 s  .  co m

    DBCursor cursor = dbCollection.find(query);
    return cursor;
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public <T extends Model> Iterator<T> findQ(Class<T> clazz, DBObject query) {

    DBCollection dbCollection = this.getCollection(clazz);
    MongoModelDeserializer modelDeserializer = this.getDeserializer(clazz);

    if (dbCollection == null) {
        LOG.warn("No collection found for clazz [{}]", clazz.getName());
        return new MongoIterator<T>(modelDeserializer, null);
    }/*from  w  w w. j a va2s .  com*/

    DBCursor cursor = dbCollection.find(query);

    return new MongoIterator<T>(modelDeserializer, cursor);
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public DBObject mapReduce(int key, String property, Filter filter, List<Integer> bins) {
    LOG.debug("Starting mapReduce for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();
    String map = "";
    String map2 = "";
    String reduce = "";
    if (propType.equals(PropertyType.STRING.toString()) || propType.equals(PropertyType.BOOL.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)\n" + "        {\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n" + "                emit({\n"
                + "                    property: property,\n"
                + "                    value: metadataRecord.sourcedValues[0].value\n"
                + "                }, 1);\n" + "\n" + "            }\n" + "            return;\n"
                + "        }\n" + "    }\n" + "    emit({\n" + "        property: property,\n"
                + "        value: 'Unknown'\n" + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.INTEGER.toString())
            || propType.equals(PropertyType.FLOAT.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    thresholds = "
                + getBinThresholds(bins) + ";\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n"
                + "                var val=metadataRecord.sourcedValues[0].value;\n"
                + "                var skipped=false;\n" + "                if (thresholds.length > 0)\n"
                + "                    for (t in thresholds){\n"
                + "                        threshold = thresholds[t];  \n"
                + "                        if (val>=threshold[0] && val<=threshold[1]){\n"
                + "                             emit({\n"
                + "                                property: property,\n"
                + "                                value: threshold[0]+'-'+threshold[1]\n"
                + "                            }, 1);\n" + "                             skipped=true;\n"
                + "                             break;\n" + "                         }\n"
                + "                    }\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";
        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.DATE.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "            if (metadataRecord.status == 'CONFLICT'){\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: 'CONFLICT'\n"
                + "                }, 1);\n" + "            } else {\n"
                + "                var date = new Date(metadataRecord.sourcedValues[0].value);\n"
                + "                var val=date.getFullYear();\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: val\n"
                + "                }, 1);\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    }//from   w w  w.j  a v  a  2s  . c  o m
    DBObject query = this.getCachedFilter(filter);
    LOG.debug("Filter query is:\n{}", query);
    String queryString = query.toString();
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);
    MapReduceOutput output = elmnts.mapReduce(cmd);
    // List<BasicDBObject> results = (List<BasicDBObject>) output.getCommandResult().get( "results" );
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }

    LOG.debug("MapReduce produced {} results", results.size());
    DBCollection histCollection = this.db.getCollection(TBL_HISTOGRAMS);
    BasicDBObject old = new BasicDBObject("_id", key);
    BasicDBObject res = new BasicDBObject(old.toMap());
    res.put("results", results);
    histCollection.update(old, res, true, false);

    DBCursor cursor = histCollection.find(new BasicDBObject("_id", key));

    if (cursor.count() == 0) {
        return null;
    }
    long end = System.currentTimeMillis();
    LOG.debug("MapReduce took {} seconds", (end - start) / 1000);
    return (DBObject) cursor.next().get("results");
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public DBObject mapReduceAllValues(int key, String property, Filter filter, List<Integer> bins) {
    LOG.debug("Starting mapReduce for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();
    String map = "";
    String map2 = "";
    String reduce = "";
    if (propType.equals(PropertyType.STRING.toString()) || propType.equals(PropertyType.BOOL.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)\n" + "        {\n"
                + "            for (i in metadataRecord.sourcedValues)\n" + "            {\n"
                + "                sv=metadataRecord.sourcedValues[i];\n" + "                emit({\n"
                + "                    property: property,\n" + "                    value: sv.value\n"
                + "                }, 1);\n" + "\n" + "            }\n" + "            return;\n"
                + "        }\n" + "    }\n" + "    emit({\n" + "        property: property,\n"
                + "        value: 'Unknown'\n" + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.INTEGER.toString())
            || propType.equals(PropertyType.FLOAT.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    thresholds = "
                + getBinThresholds(bins) + ";\n" + "    for (mr in this.metadata)" + "    {\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property)" + "        {\n"
                + "           for (i in metadataRecord.sourcedValues)" + "           {\n"
                + "                sv=metadataRecord.sourcedValues[i];\n"
                + "                var val=sv.value;\n" + "                if (thresholds.length > 0)\n"
                + "                    for (t in thresholds){\n"
                + "                        threshold = thresholds[t];  \n"
                + "                        if (val>=threshold[0] && val<=threshold[1]){\n"
                + "                             emit({\n"
                + "                                property: property,\n"
                + "                                value: threshold[0]+'-'+threshold[1]\n"
                + "                            }, 1);\n" + "                         }\n"
                + "                    }\n" + "            }\n" + "            return;\n" + "         }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";
        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    } else if (propType.equals(PropertyType.DATE.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n"
                + "           for (i in metadataRecord.sourcedValues){\n"
                + "               sv=metadataRecord.sourcedValues[i];\n"
                + "               var date = new Date(sv.value);\n"
                + "               var val=date.getFullYear();\n" + "               emit({\n"
                + "                    property: property,\n" + "                    value: val\n"
                + "               }, 1);\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}";

        reduce = "function reduce(key, values) {\n" + "    var res = 0;\n"
                + "    values.forEach(function(v) {\n" + "        res += v;\n" + "    });\n"
                + "    return res;\n" + "}";

    }/*from w  w  w.ja  v a2  s.  co  m*/
    DBObject query = this.getCachedFilter(filter);
    LOG.debug("Filter query is:\n{}", query);
    String queryString = query.toString();
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);
    MapReduceOutput output = elmnts.mapReduce(cmd);
    // List<BasicDBObject> results = (List<BasicDBObject>) output.getCommandResult().get( "results" );
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }

    LOG.debug("MapReduce produced {} results", results.size());
    DBCollection histCollection = this.db.getCollection(TBL_HISTOGRAMS);
    BasicDBObject old = new BasicDBObject("_id", key);
    BasicDBObject res = new BasicDBObject(old.toMap());
    res.put("results", results);
    histCollection.update(old, res, true, false);

    DBCursor cursor = histCollection.find(new BasicDBObject("_id", key));

    if (cursor.count() == 0) {
        return null;
    }
    long end = System.currentTimeMillis();
    LOG.debug("MapReduce took {} seconds", (end - start) / 1000);
    return (DBObject) cursor.next().get("results");
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public DBObject mapReduceStats(int key, String property, Filter filter) {
    LOG.debug("Starting mapReduceStats for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();
    String map = "";
    String reduce = "";
    String finalize = "";
    if (propType.equals(PropertyType.INTEGER.toString()) || propType.equals(PropertyType.FLOAT.toString())) {
        map = "function() {\n" + "    property = '" + property + "';\n" + "    for (mr in this.metadata){\n"
                + "        metadataRecord=this.metadata[mr];\n"
                + "        if(metadataRecord.property == property){\n" + "            {\n"
                + "                emit({\n" + "                    property: property,\n"
                + "                    value: property\n" + "                }, \n" + "                {\n"
                + "                    sum: metadataRecord.sourcedValues[0].value,\n"
                + "                    min: metadataRecord.sourcedValues[0].value,\n"
                + "                    max: metadataRecord.sourcedValues[0].value,\n"
                + "                    count: 1,\n" + "                    diff: 0\n" + "                }\n"
                + "                )\n" + "            }\n" + "            return;\n" + "        }\n"
                + "    }\n" + "    emit({\n" + "        property: property,\n" + "        value: 'Unknown'\n"
                + "        }, 1);\n" + "}\n";
        reduce = "function reduce(key, values) {\n" + "var a = values[0];\n"
                + "        for (var i = 1; i < values.length; i++) {\n" + "            var b = values[i];\n"
                + "            var delta = a.sum / a.count - b.sum / b.count;\n"
                + "            var weight = (a.count * b.count) / (a.count + b.count);\n"
                + "            a.diff += b.diff + delta * delta * weight;\n"
                + "            a.sum = b.sum*1+ a.sum*1;\n" + "            a.count += b.count;\n"
                + "            a.min = Math.min(a.min, b.min);\n"
                + "            a.max = Math.max(a.max, b.max);\n" + "        }\n" + "return a;" + "}"

        ;//  ww w.  j a  v a  2s  .c o  m
        finalize = "function finalize(key, value) {\n" + "    value.avg = value.sum / value.count;\n"
                + "    value.variance = value.diff / value.count;\n"
                + "    value.stddev = Math.sqrt(value.variance);\n" + "    return value;\n" + "}";

    }
    DBObject query = this.getCachedFilter(filter);
    LOG.debug("filter query is:\n{}", query);
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);
    cmd.setFinalize(finalize);
    MapReduceOutput output = elmnts.mapReduce(cmd);

    //List<BasicDBObject> results = (List<BasicDBObject>) output.getCommandResult().get( "results" );
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }

    LOG.debug("MapReduce produced {} results", results.size());
    DBCollection histCollection = this.db.getCollection(TBL_HISTOGRAMS);
    BasicDBObject old = new BasicDBObject("_id", key);
    BasicDBObject res = new BasicDBObject(old.toMap());
    res.put("results", results);
    histCollection.update(old, res, true, false);

    DBCursor cursor = histCollection.find(new BasicDBObject("_id", key));

    if (cursor.count() == 0) {
        return null;
    }
    long end = System.currentTimeMillis();
    LOG.debug("The map-reduce job took {} seconds", (end - start) / 1000);
    return (DBObject) cursor.next().get("results");
}

From source file:com.ratzia.pfc.webpageanalyticaltool.dbupdater.Watcher.java

public void processDatabase(LinkedList<String> pluginsCode, String currentVersion) throws UnknownHostException {
    /**** Plugins ****/
    LinkedList<SysPlugin> plugins = loadPlugins(pluginsCode);
    /*****************/

    /*SecurityManager oldSecurityManager = System.getSecurityManager();
    DBPluginSecurityManager dbPluginSecurityManager = new DBPluginSecurityManager();
    System.setSecurityManager(dbPluginSecurityManager);
    //Will open the security manager so we need to ensure it is closed afterwards
    try{*///from  w  w w .  j a  v a 2s  .c  om
    MongoClient mongoClient = new MongoClient(serverAddress);
    DB local = mongoClient.getDB(db);
    DBCollection dbCol = local.getCollection(collection);
    BasicDBObject query = new BasicDBObject();
    query.put(PLUGIN_VERSION_FIELD, new BasicDBObject("$ne", currentVersion));
    DBCursor cursor = dbCol.find(query);
    long count = 0;
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();

        //Copy contents
        BasicDBObject res = new BasicDBObject();
        for (String k : obj.keySet()) {
            res.put(k, obj.get(k));
        }

        //Plugin operations
        for (SysPlugin plugin : plugins) {
            try {
                plugin.run(res);
            } catch (Exception ex) {
                System.out.println("Error en " + plugin.getClass());
                Logger.getLogger(Watcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //Put plugin only fields into the original object
        for (String k : res.keySet()) {
            if ((k.substring(0, 2)).compareTo("p_") == 0) {
                obj.put(k, res.get(k));
            }
        }

        //Update version on object
        obj.put(PLUGIN_VERSION_FIELD, currentVersion);

        dbCol.save(obj);
        count++;
    }
    cursor.close();

    if (count > 0) {
        System.out.println(count + " updated");
    }

    /*}catch(Exception ex){
    Logger.getLogger(Watcher.class.getName()).log(Level.SEVERE, null, ex);
    }
    //close sandbox
    System.setSecurityManager(oldSecurityManager);*/

}

From source file:com.redhat.lightblue.mongo.crud.MongoCRUDController.java

License:Open Source License

protected void populateHiddenFields(EntityInfo ei, Metadata md, String version, List<Path> fields,
        QueryExpression query) throws IOException {
    LOGGER.info("Starting population of hidden fields due to new or modified indexes.");
    MongoDataStore ds = (MongoDataStore) ei.getDataStore();
    DB entityDB = dbResolver.get(ds);/*from   www . j  a v  a2s.com*/
    DBCollection coll = entityDB.getCollection(ds.getCollectionName());
    DBCursor cursor = null;
    try {
        if (query != null) {
            MetadataResolver mdResolver = new MetadataResolver() {
                @Override
                public EntityMetadata getEntityMetadata(String entityName) {
                    String v = version == null ? ei.getDefaultVersion() : version;
                    return md.getEntityMetadata(entityName, v);
                }
            };
            ExpressionTranslator trans = new ExpressionTranslator(mdResolver, JsonNodeFactory.instance);
            DBObject mongoQuery = trans.translate(mdResolver.getEntityMetadata(ei.getName()), query);
            cursor = coll.find(mongoQuery);
        } else {
            cursor = coll.find();
        }
        while (cursor.hasNext()) {
            DBObject doc = cursor.next();
            DBObject original = (DBObject) ((BasicDBObject) doc).copy();
            try {
                DocTranslator.populateDocHiddenFields(doc, fields);
                LOGGER.debug("Original doc:{}, new doc:{}", original, doc);
                if (!doc.equals(original)) {
                    coll.save(doc);
                }
            } catch (Exception e) {
                // skip the doc if there's a problem, don't outright fail
                LOGGER.error(e.getMessage());
                LOGGER.debug("Original doc:\n{}", original);
                LOGGER.debug("Error saving doc:\n{}", doc);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error during reindexing");
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    } finally {
        cursor.close();
    }
    LOGGER.info("Finished population of hidden fields.");
}

From source file:com.restfeel.controller.rest.EntityDataController.java

License:Apache License

@RequestMapping(value = "/api/{projectId}/entities/{name}/list", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody String getEntityDataList(@PathVariable("projectId") String projectId,
        @PathVariable("name") String entityName, @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "sort", required = false) String sort,
        @RequestParam(value = "query", required = false) String query,
        @RequestHeader(value = "authToken", required = false) String authToken) {

    JSONObject authRes = authService.authorize(projectId, authToken, "USER");
    if (!authRes.getBoolean(SUCCESS)) {
        return authRes.toString(4);
    }//from www .  java 2 s  . c  o  m

    DBCollection dbCollection = mongoTemplate.getCollection(projectId + "_" + entityName);
    DBCursor cursor;
    if (query != null && !query.isEmpty()) {
        Object queryObject = JSON.parse(query);
        cursor = dbCollection.find((BasicDBObject) queryObject);
    } else {
        cursor = dbCollection.find();
    }

    if (sort != null && !sort.isEmpty()) {
        Object sortObject = JSON.parse(sort);
        cursor.sort((BasicDBObject) sortObject);
    }

    if (limit != null && limit > 0) {
        if (page != null && page > 0) {
            cursor.skip((page - 1) * limit);
        }
        cursor.limit(limit);
    }
    List<DBObject> array = cursor.toArray();

    if (entityName.equals("User")) {
        for (DBObject dbObject : array) {
            dbObject.removeField(PASSWORD);
        }
    }

    for (DBObject dbObject : array) {
        dbRefToRelation(dbObject);
    }
    String json = JSON.serialize(array);

    // Indentation
    JSONArray jsonArr = new JSONArray(json);
    return jsonArr.toString(4);
}

From source file:com.restfiddle.controller.rest.EntityDataController.java

License:Apache License

@RequestMapping(value = "/api/{projectId}/entities/{name}/list", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody String getEntityDataList(@PathVariable("projectId") String projectId,
        @PathVariable("name") String entityName, @RequestParam(value = "page", required = false) Integer page,
        @RequestParam(value = "limit", required = false) Integer limit,
        @RequestParam(value = "sort", required = false) String sort,
        @RequestParam(value = "query", required = false) String query) {
    DBCollection dbCollection = mongoTemplate.getCollection(entityName);
    DBCursor cursor = null;/*from ww w  .  j av  a 2 s.  com*/
    if (query != null && !query.isEmpty()) {
        Object queryObject = JSON.parse(query);
        cursor = dbCollection.find((BasicDBObject) queryObject);
    } else {
        cursor = dbCollection.find();
    }

    if (sort != null && !sort.isEmpty()) {
        Object sortObject = JSON.parse(sort);
        cursor.sort((BasicDBObject) sortObject);
    }

    if (limit != null && limit > 0) {
        if (page != null && page > 0) {
            cursor.skip((page - 1) * limit);
        }
        cursor.limit(limit);
    }
    List<DBObject> array = cursor.toArray();
    String json = JSON.serialize(array);

    // Indentation
    JSONArray jsonArr = new JSONArray(json);
    return jsonArr.toString(4);
}

From source file:com.ricardolorenzo.identity.user.impl.UserIdentityManagerMongoDB.java

License:Open Source License

/**
 * All the scripts should have the following format:
 *
 * {//from  w  w w.j  av a 2s.  c om
 *    database.collection: {
 *        operation: insert|update|find|aggregate|delete
 *        query: {}
 *    }
 * }
 *
 * For update operations, you should specify the following:
 *
 * query: {
 *     find: {}
 *     update: {}
 * }
 */
private List<DBObject> runQueryScript(final String scriptType, final Map<String, Object[]> attributes)
        throws IdentityException {
    List<DBObject> results = new ArrayList<>();
    try {
        DB database = mongoClient.getDB(this.properties.getProperty("mongodb.database"));
        final ScriptCollection sc = getScriptCollection();
        if (sc.hasScript(scriptType)) {
            final String scriptContent = sc.getScript(scriptType);
            String query = createQueryFromScript(scriptContent, attributes);
            DBObject collectionOperation = DBObject.class.cast(JSON.parse(query));

            for (String collection : collectionOperation.keySet()) {
                if (!database.collectionExists(collection)) {
                    throw new IdentityException("collection [" + collection + "] does not exists");
                }

                DBObject dbObject = DBObject.class.cast(collectionOperation.get(collection));
                if (!dbObject.containsField("operation")) {
                    throw new IdentityException("operation field not specified");
                }

                String dbOperation = String.class.cast(dbObject.get("operation")).toLowerCase();
                if (!OPERATIONS.contains(dbOperation)) {
                    throw new IdentityException("operation [" + dbOperation + "] not supported");
                }

                DBObject dbQuery = DBObject.class.cast(dbObject.get("query"));
                if (dbQuery == null) {
                    throw new IdentityException("query field not specified");
                }

                DBCollection coll = database.getCollection(collection);
                switch (dbOperation) {
                case "insert": {
                    coll.insert(dbQuery);
                }
                case "update": {
                    if (!dbObject.containsField("find")) {
                        throw new IdentityException("find field not found inside the update operation");
                    }
                    if (!dbObject.containsField("update")) {
                        throw new IdentityException("update field not found inside the update operation");
                    }
                    DBObject dbUpdateFind = DBObject.class.cast(dbQuery.get("find"));
                    DBObject dbUpdateFields = DBObject.class.cast(dbQuery.get("update"));
                    coll.update(dbUpdateFind, dbUpdateFields, false, false);
                }
                case "delete": {
                    coll.remove(dbQuery);
                }
                case "find": {
                    DBCursor cursor = coll.find(dbQuery);
                    while (cursor.hasNext()) {
                        results.add(cursor.next());
                    }
                }
                case "aggregate": {
                    List<DBObject> aggregate = new ArrayList<DBObject>();
                    aggregate.add(dbQuery);
                    for (DBObject o : coll.aggregate(aggregate).results()) {
                        results.add(o);
                    }
                }
                }
            }
            return results;
        }
    } catch (final NoSuchAlgorithmException e) {
        throw new IdentityException(e.getMessage());
    } finally {
        /**
         * TODO close cursors
         */
    }
    return null;
}