Example usage for com.mongodb DBCursor length

List of usage examples for com.mongodb DBCursor length

Introduction

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

Prototype

public int length() 

Source Link

Document

Pulls back all items into an array and returns the number of objects.

Usage

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

License:Apache License

void updatePurgeTracker(long purgeId, String purgeCollName, EzSecurityToken token) {
    appLog.info("updatePurgeTracker with Purge Id {} and Collection Name {}", purgeId, purgeCollName);
    String trackingPurgeCollName = getCollectionName(PURGE_TRACKING_COLL_NAME);
    DBObject query = new BasicDBObject();
    query.put(RedactHelper.APP_ID_FIELD, appId);
    query.put(RedactHelper.PURGE_ID, purgeId);
    DBCursor cursor = db.getCollection(trackingPurgeCollName).find(query);
    if (cursor.length() == 0) {
        //insert//from  w w  w.  jav a2s .co  m
        appLog.info("No Records Found for Purge Id {} in Purge Collection {}", purgeId, trackingPurgeCollName);

        try {
            JSONObject jobj = new JSONObject();
            jobj.put(RedactHelper.PURGE_ID, purgeId);
            jobj.put(RedactHelper.PURGE_TRACKING_COLL_FIELD, purgeCollName);
            jobj.put(RedactHelper.APP_ID_FIELD, appId);
            appLog.info("Dumping JSON before Insert : {}", jobj.toString());
            this.insert(PURGE_TRACKING_COLL_NAME,
                    new MongoEzbakeDocument(jobj.toString(), new Visibility().setFormalVisibility("S")), token);

        } catch (TException e) {
            e.printStackTrace();
        } catch (org.codehaus.jettison.json.JSONException je) {
            je.printStackTrace();
            appLog.error("Error updating the purge record!");
        }

    } else {
        //update
        appLog.info("Updating Purge Collection {} with Purge Id", PURGE_TRACKING_COLL_NAME, purgeId);
        DBObject content = new BasicDBObject();
        content.put(RedactHelper.PURGE_IDS_FIELD, purgeId);
        content.put(RedactHelper.PURGE_TRACKING_COLL_FIELD, purgeCollName);
        mongoUpdateHelper.updateContent(getCollectionName(PURGE_TRACKING_COLL_NAME), query, content, false,
                false);
    }

}

From source file:org.javaee7.extra.mongo.PersonSessionBean.java

License:Open Source License

public List<Person> getPersons() {
    List<Person> persons = new ArrayList();
    DBCursor cur = personCollection.find();
    System.out.println("getPersons: Found " + cur.length() + " person(s)");
    for (DBObject dbo : cur.toArray()) {
        persons.add(Person.fromDBObject(dbo));
    }//w  w  w  . ja v a 2 s . c om

    return persons;
}