Example usage for com.mongodb DBCursor count

List of usage examples for com.mongodb DBCursor count

Introduction

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

Prototype

public int count() 

Source Link

Document

Counts the number of objects matching the query.

Usage

From source file:com.andreig.jetty.QueryServlet.java

License:GNU General Public License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doGet()");

    if (!can_read(req)) {
        res.sendError(SC_UNAUTHORIZED);//from   w  ww. j av a2  s  .c o m
        return;
    }

    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            col_name = names[1];
        }
        if (db_name == null || col_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }
    String skip = req.getParameter("skip");
    String limit = req.getParameter("limit");

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    DBCursor c = col.find();
    if (c == null || c.count() == 0) {
        error(res, SC_NOT_FOUND, Status.get("no documents found"));
        return;
    }

    res.setIntHeader("X-Documents-Count", c.count());

    if (limit != null) {
        try {
            c.limit(Math.min(Integer.parseInt(limit), MAX_FIELDS_TO_RETURN));
        } catch (NumberFormatException e) {
            error(res, SC_BAD_REQUEST, Status.get("can not parse limit"));
            c.close();
            return;
        }
    } else
        c.limit(MAX_FIELDS_TO_RETURN);

    if (skip != null) {
        try {
            c.skip(Integer.parseInt(skip));
        } catch (NumberFormatException e) {
            error(res, SC_BAD_REQUEST, Status.get("can not parse skip"));
            c.close();
            return;
        }
    }

    StringBuilder buf = tl.get();
    buf.setLength(0);

    int no = 0;
    buf.append("[");
    while (c.hasNext()) {

        DBObject o = c.next();
        if (rm_id)
            o.removeField("_id");
        JSON.serialize(o, buf);
        buf.append(",");
        no++;

    }
    c.close();

    if (no > 0)
        buf.setCharAt(buf.length() - 1, ']');
    else
        buf.append(']');

    res.setIntHeader("X-Documents-Returned", no);

    out_str(req, buf.toString(), "application/json");

}

From source file:com.andreig.jetty.SearchServlet.java

License:GNU General Public License

private String search2mongo(Document hits[], DBCollection col) {

    List<ObjectId> values = new ArrayList<ObjectId>();
    for (Document hit : hits) {
        String _id = hit.get("_id");
        ObjectId oid = ObjectId.massageToObjectId(_id);
        values.add(oid);//www . j a v  a2s.c o  m
    }

    BasicDBObject q = new BasicDBObject();
    q.put("_id", new BasicDBObject("$in", values));

    DBCursor c = col.find(q);
    if (c == null || c.count() == 0) {
        return null;
    }

    StringBuilder buf = tl.get();
    // reset buf
    buf.setLength(0);

    int no = 0;
    buf.append("[");
    while (c.hasNext()) {

        DBObject o = c.next();
        if (rm_id)
            o.removeField("_id");
        JSON.serialize(o, buf);
        buf.append(",");
        no++;

    }
    c.close();

    if (no > 0)
        buf.setCharAt(buf.length() - 1, ']');
    else
        buf.append(']');

    return buf.toString();

}

From source file:com.andreig.jetty.WriteServlet.java

License:GNU General Public License

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doDelete()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);// ww w  .  j a v  a2  s .  c om
        return;
    }

    InputStream is = req.getInputStream();
    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            col_name = names[1];
        }
        if (db_name == null || col_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    BufferedReader r = null;
    DBObject q = null;
    try {

        r = new BufferedReader(new InputStreamReader(is));
        String data = r.readLine();
        if (data == null) {
            error(res, SC_BAD_REQUEST, Status.get("no data"));
            return;
        }
        try {
            q = (DBObject) JSON.parse(data);
        } catch (JSONParseException e) {
            error(res, SC_BAD_REQUEST, Status.get("can not parse data"));
            return;
        }

    } finally {
        if (r != null)
            r.close();
    }

    // search
    if (do_search) {

        DBCursor c = col.find(q);
        long l = c.count();
        String todelete[] = new String[(int) l];
        int n = 0;

        while (c.hasNext()) {

            DBObject o = c.next();
            ObjectId oid = (ObjectId) o.get("_id");
            String id = oid.toStringMongod();
            todelete[n++] = id;

        }
        c.close();
        search.get_writer().delete(todelete);

    }

    WriteResult wr = col.remove(q, write_concern);

    // return operation status
    if (do_return) {
        out_str(req, wr.toString());
        if (wr.getError() == null) {
            res.setStatus(SC_BAD_REQUEST);
            return;
        }
    }

    res.setStatus(SC_OK);

}

From source file:com.andreig.jetty.WriteServlet.java

License:GNU General Public License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPost()");

    if (!can_write(req)) {
        res.sendError(SC_UNAUTHORIZED);//  w w w. ja  v  a2s .  co  m
        return;
    }

    InputStream is = req.getInputStream();
    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];
            col_name = names[1];
        }
        if (db_name == null || col_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    boolean upsert = Boolean.parseBoolean(req.getParameter("upsert"));
    boolean multi = Boolean.parseBoolean(req.getParameter("multi"));

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    BufferedReader r = null;
    DBObject q = null, o = null;
    try {

        r = new BufferedReader(new InputStreamReader(is));
        String q_s = r.readLine();
        if (q_s == null) {
            error(res, SC_BAD_REQUEST, Status.get("no data"));
            return;
        }
        String o_s = r.readLine();
        if (o_s == null) {
            error(res, SC_BAD_REQUEST, Status.get("obj to update missing"));
            return;
        }
        try {
            q = (DBObject) JSON.parse(q_s);
            o = (DBObject) JSON.parse(o_s);
        } catch (JSONParseException e) {
            error(res, SC_BAD_REQUEST, Status.get("can not parse data"));
            return;
        }

    } finally {
        if (r != null)
            r.close();
    }
    //
    // search
    if (do_search) {

        String fn = col.getFullName();
        DBCursor c = col.find(q);
        int cnt = c.count();
        if (!multi)
            c.limit(1);
        long l = multi ? cnt : 1;
        String toupdate[] = new String[(int) l];
        int n = 0;
        boolean insert = false;

        if (upsert && !multi && cnt == 0)
            insert = true;

        while (c.hasNext()) {

            DBObject _o = c.next();
            ObjectId oid = (ObjectId) _o.get("_id");
            String id = oid.toStringMongod();
            toupdate[n++] = id;

        }
        c.close();

        List<String> flds = Config.search_index_fields.get(fn);
        boolean commit = false;
        Document doc = null;
        Search _writer = search.get_writer();
        if (flds != null && flds.size() > 0) {
            doc = new Document();
            try {
                for (String fld : flds) {
                    String val = (String) o.get(fld);
                    if (val == null)
                        continue;
                    Search.add_searchable_s(doc, fld, val);
                    commit = true;
                }
                if (commit)
                    _writer.commit(doc);
            } catch (ClassCastException e) {
                error(res, SC_BAD_REQUEST, Status.get("searchable fields must be type String"));
                return;
            } catch (CorruptIndexException e) {
                error(res, SC_BAD_REQUEST, Status.get("Search corrupt index" + e));
                return;
            }
        }
        if (commit && insert)
            log.warning("upsert with search not implemented yet");
        else
            _writer.update(toupdate, doc);

    }

    WriteResult wr = col.update(q, o, upsert, multi, write_concern);

    // return operation status
    if (do_return) {
        out_str(req, wr.toString());
        if (wr.getError() == null) {
            res.setStatus(SC_BAD_REQUEST);
            return;
        }
    }

    res.setStatus(SC_CREATED);

}

From source file:com.bbc.remarc.ws.AudioServiceImpl.java

License:Apache License

@GET
@Produces("application/json")
@Path("/metadata/{id}")
public Response getAudioMetadata(@PathParam("id") String id) {
    log.trace("getAudioMetadata");

    DB db = MongoClient.getDB();/*  w w w.  j  a  va 2 s .  c o m*/
    DBCursor results = null;

    try {
        db.requestStart();
        results = db.getCollection(COLLECTION_NAME).find(BasicDBObjectBuilder.start().add("id", id).get());

        if (results.count() == 0) {
            return Response.status(404).build();
        }

    } finally {
        db.requestDone();
    }

    return Response.ok(results.next().toString()).build();
}

From source file:com.bbc.remarc.ws.ImageServiceImpl.java

License:Apache License

@GET
@Produces("application/json")
@Path("/metadata/{id}")
public Response getImageMetadata(@PathParam("id") String id) {
    log.trace("getImageMetadata");

    DB db = MongoClient.getDB();//from w w  w .jav  a 2s.co m
    DBCursor results = null;

    try {
        db.requestStart();
        results = db.getCollection(COLLECTION_NAME).find(BasicDBObjectBuilder.start().add("id", id).get());

        if (results.count() == 0) {
            return Response.status(404).build();
        }

    } finally {
        db.requestDone();
    }

    return Response.ok(results.next().toString()).build();
}

From source file:com.bbc.remarc.ws.MetadataService.java

License:Apache License

/**
 * Core random find service for entities that have metadata
 * /*  w ww  .  j a va 2s  .  c om*/
 * @param db
 *            db instance
 * @param limit
 *            number of records to find
 * @param filter
 *            find records that match this filter. Can be null for no
 *            filtering
 * @param collectionName
 *            the database collection name
 * @return collection of found DBObjects
 */
protected List<DBObject> findRandom(DB db, long limit, DBObject filter, final String collectionName) {

    List<DBObject> results = new ArrayList<DBObject>();

    try {
        db.requestStart();
        Random random = new Random();
        DBCollection dbCollection = db.getCollection(collectionName);

        long maxRecords = dbCollection.count();

        if (limit > maxRecords) {
            limit = maxRecords;
        }

        for (int i = 0; i < limit; i++) {

            boolean foundUnique = false;

            int numChecked = 0;

            while (!foundUnique) {

                if (numChecked == limit) {
                    break;
                }

                int randomNumber = random.nextInt((int) dbCollection.count());

                DBObject randomRecord = null;

                if (filter == null) {
                    randomRecord = dbCollection.find().limit(-1).skip(randomNumber).next();
                } else {

                    DBCursor cursor = dbCollection.find(filter).limit(-1);

                    if (i >= cursor.count()) {
                        // we dont have enough records from the filter to
                        // match the limit
                        break;
                    }

                    randomNumber = random.nextInt((int) cursor.count());

                    randomRecord = cursor.skip(randomNumber).next();
                }

                foundUnique = resultsContains(randomRecord, results) == false;

                if (foundUnique) {
                    randomRecord.removeField("_id");
                    results.add(randomRecord);
                }
            }
        }
    } finally {
        db.requestDone();
    }
    return results;
}

From source file:com.bbc.remarc.ws.VideoServiceImpl.java

License:Apache License

@GET
@Produces("application/json")
@Path("/metadata/{id}")
public Response getVideoMetadata(@PathParam("id") String id) {
    log.trace("getAudioMetadata");

    DB db = MongoClient.getDB();/*w w w .  j av  a2s .  com*/
    DBCursor results = null;

    try {
        db.requestStart();
        results = db.getCollection(COLLECTION_NAME).find(BasicDBObjectBuilder.start().add("id", id).get());

        if (results.count() == 0) {
            return Response.status(404).build();
        }

    } finally {
        db.requestDone();
    }

    return Response.ok(results.next().toString()).build();
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Calculates shard chunks./*from ww  w .  j a v  a2s . c  om*/
 *
 * @param collection the collection
 * @return the deep partition [ ]
 */
private HadoopPartition[] calculateShardChunks(DBCollection collection) {

    DBCursor chuncks = getChunks(collection);

    Map<String, String[]> shards = getShards(collection);

    MongoPartition[] deepPartitions = new MongoPartition[chuncks.count()];
    int i = 0;
    boolean keyAssigned = false;
    String key = null;
    while (chuncks.hasNext()) {

        DBObject dbObject = chuncks.next();
        if (!keyAssigned) {
            Set<String> keySet = ((DBObject) dbObject.get("min")).keySet();
            for (String s : keySet) {
                key = s;
                keyAssigned = true;
            }
        }
        deepPartitions[i] = new MongoPartition(
                mongoDeepJobConfig.getRddId(), i, new TokenRange(shards.get(dbObject.get("shard")),
                        ((DBObject) dbObject.get("min")).get(key), ((DBObject) dbObject.get("max")).get(key)),
                key);
        i++;
    }
    List<MongoPartition> mongoPartitions = Arrays.asList(deepPartitions);

    Collections.shuffle(mongoPartitions);
    return mongoPartitions.toArray(new MongoPartition[mongoPartitions.size()]);
}

From source file:com.caci.dummyserver.MongoRepository.java

public GetObjectsResult getObjects(String table, Collection<Pair<String, String>> keys,
        Collection<String> fields, Collection<Pair<String, Collection<String>>> filters,
        Collection<String> sort, Integer offset, Integer limit) {
    DB db = mongo.getDB("Objects");
    DBCollection col = db.getCollection(table);

    GetObjectsResult result = new GetObjectsResult();

    DBObject queryObject = makeQueryObject(keys, filters);
    DBCursor cursor = col.find(queryObject);
    try {/*from   w  w  w  . j ava 2s  .  c  o m*/
        if (sort != null && !sort.isEmpty()) {
            DBObject sortObj = new BasicDBObject();
            for (String fieldName : sort) {
                if (fieldName.startsWith("-")) {
                    sortObj.put("data." + fieldName.substring(1), -1);
                } else {
                    sortObj.put("data." + fieldName, 1);
                }
            }
            cursor.sort(sortObj);
        }

        result.setTotal(cursor.count());

        if (offset != null && offset > 0) {
            cursor.skip(offset);
        }
        if (limit != null && limit > 0) {
            cursor.limit(limit);
        }

        while (cursor.hasNext()) {
            DBObject obj = cursor.next();
            obj = pruneDBObject(getData(obj), fields);
            result.getResults().add(obj.toString());
        }
    } finally {
        cursor.close();
    }

    return result;
}