Example usage for com.mongodb.client MongoCursor hasNext

List of usage examples for com.mongodb.client MongoCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb.client MongoCursor hasNext.

Prototype

@Override
    boolean hasNext();

Source Link

Usage

From source file:org.helm.rest.MongoDB.java

public long[] SelectList(String table, String key, BsonDocument where) {
    key = key.toLowerCase();//from  w  ww .ja va  2 s.c om

    MongoCollection coll = db.getCollection(table);
    Document fields = new Document("_id", false);
    fields.append(key, true);
    MongoCursor cur = (where == null ? coll.find() : coll.find(where)).projection(fields).iterator();
    if (cur == null || !cur.hasNext())
        return null;

    List<Long> list = new ArrayList();
    while (cur.hasNext()) {
        Document d = (Document) cur.next();
        list.add((long) d.get(key));
    }

    long[] ret = new long[list.size()];
    for (int i = 0; i < ret.length; ++i)
        ret[i] = list.get(i);
    return ret;
}

From source file:org.helm.rest.MongoDB.java

public String SelectString(String table, String key, BsonDocument where) {
    key = key.toLowerCase();/*from   w  w w . j  a  v  a  2 s  .c  o m*/

    MongoCollection coll = db.getCollection(table);
    Document fields = new Document("_id", false);
    fields.append(key, true);
    MongoCursor cur = (where == null ? coll.find() : coll.find(where)).limit(1).projection(fields).iterator();
    if (cur == null || !cur.hasNext())
        return null;

    Document d = (Document) cur.next();
    return d.get(key) + "";
}

From source file:org.helm.rest.MongoDB.java

public ArrayList<JSONObject> ReadAsJson(String table, String cols, BsonDocument where) {
    FindIterable iter;/* w  w w . j  a v  a 2 s  .c om*/
    MongoCollection coll = db.getCollection(table);
    if (where == null) {
        iter = coll.find();
    } else {
        iter = coll.find(where);
    }

    if (cols != null) {
        String[] ss = cols.split(",");
        Document fields = new Document("_id", false);
        for (int i = 0; i < ss.length; ++i) {
            fields.append(ss[i].trim().toLowerCase(), true);
        }
        iter = iter.projection(fields);
    }

    MongoCursor cur = iter.iterator();
    if (cur == null || !cur.hasNext())
        return null;
    return ResultSet2Json(cur);
}

From source file:org.helm.rest.MongoDB.java

public String ReadAsSDF(String table, String molfilekey) {
    MongoCollection coll = db.getCollection(table);
    FindIterable iter = coll.find();//from   w  w  w  .  j a  va2 s .c o  m

    String lb = System.getProperty("line.separator");
    molfilekey = molfilekey.toLowerCase();
    StringBuilder sb = new StringBuilder();

    if (iter == null) {
        return null;
    } else {
        Document fields = new Document("_id", false);
        iter = iter.projection(fields);
    }

    MongoCursor cur = iter.iterator();
    while (cur.hasNext()) {
        Document doc = (Document) cur.next();
        String m = doc.getString(molfilekey);
        if (m != null) {
            // the molfile from toolkit has extra $$$$ line
            // fix bug: https://github.com/PistoiaHELM/HELMWebEditor/issues/94
            int p = m.lastIndexOf("M  END") + 6;
            if (p > 6 && p < m.length() - 1)
                m = m.substring(0, p);
        } else {
            m = lb + "   JSDraw203101711402D" + lb + lb + "  0  0  0  0  0  0              0 V2000" + lb
                    + "M  END";
        }
        sb.append(m);
        sb.append(lb);

        for (String k : doc.keySet()) {
            if (k.equals(molfilekey) || k.equals("_id"))
                continue;

            sb.append("> <");
            sb.append(k);
            sb.append(">");
            sb.append(lb);

            String s = doc.get(k) + "";
            sb.append(s == null ? "" : s);
            sb.append(lb);

            sb.append(lb);
        }
        sb.append("$$$$");
        sb.append(lb);
    }

    return sb.toString();
}

From source file:org.helm.rest.MongoDB.java

ArrayList<JSONObject> ResultSet2Json(MongoCursor rs) {
    ArrayList<JSONObject> list = new ArrayList();
    if (rs == null || !rs.hasNext())
        return list;

    try {/*  w ww.  ja va  2s. c  o m*/
        while (rs.hasNext()) {
            list.add(Result2Json(rs));
        }
    } catch (Exception e) {
        error = e;
        return null;
    }
    return list;
}

From source file:org.helm.rest.MongoDB.java

JSONObject Result2Json(MongoCursor rs) {
    JSONObject ret = new JSONObject();
    if (rs == null || !rs.hasNext())
        return ret;

    try {/* w  w w .  j  av  a  2 s. co m*/
        Document doc = (Document) rs.next();
        for (String k : doc.keySet()) {
            ret.put(k.toLowerCase(), doc.get(k));
        }
    } catch (Exception e) {
        error = e;
        return null;
    }
    return ret;
}

From source file:org.helm.rest.MongoDB.java

long GetMaxID(String table) {
    MongoCollection coll = db.getCollection(table);

    Document fields = new Document("_id", false);
    fields.append("id", true);
    MongoCursor cur = coll.find().projection(fields).iterator();
    if (cur == null || !cur.hasNext())
        return 0;

    long id = 0;/*from   ww  w  . ja v  a  2  s  .  co  m*/
    while (cur.hasNext()) {
        Document d = (Document) cur.next();
        Object t = d.get("id");
        if (t == null)
            continue;
        long i = AjaxTool.ToLong(t + "");
        if (i > id)
            id = i;
    }
    return id;
}

From source file:org.iotivity.cloud.accountserver.db.MongoDB.java

License:Open Source License

/**
 * API for selecting records from DB table.
 * // ww  w  . ja  v  a 2  s  .c  o m
 * @param tableName
 *            table name for the record to be selected
 * @param doc
 *            document filter to be selected
 * @return record list according to the filter document
 */
public ArrayList<HashMap<String, Object>> selectRecord(String tableName, Document doc) {

    if (tableName == null || doc == null)
        return null;

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(doc).iterator();

    ArrayList<HashMap<String, Object>> recordList = new ArrayList<HashMap<String, Object>>();

    try {

        while (cursor.hasNext()) {
            Document selectedDoc = cursor.next();
            recordList.add(convertDocumentToHashMap(selectedDoc));
        }

    } finally {

        cursor.close();
    }

    return recordList;
}

From source file:org.iotivity.cloud.accountserver.db.MongoDB.java

License:Open Source License

private void showRecord(String tableName) {

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find().iterator();

    Log.i("<" + tableName + ">");

    HashMap<String, Object> records = null;
    int index = 0;
    while (cursor.hasNext()) {

        Document doc = cursor.next();
        records = convertDocumentToHashMap(doc);

        Log.i("[" + index + "] " + records.toString());
        index++;//from   w w w  .j  a v  a 2  s . c  o  m
    }

    cursor.close();
}

From source file:org.iotivity.cloud.rdserver.db.MongoDB.java

License:Open Source License

/**
 * API for selecting records from DB table.
 * //from  w ww.j ava 2  s . c  om
 * @param tableName
 *            table name for the record to be selected
 * @param doc
 *            document filter to be selected
 * @return record list according to the filter document
 */
public ArrayList<HashMap<String, Object>> selectRecord(String tableName, Document doc) {

    if (tableName == null || doc == null)
        return null;

    MongoCollection<Document> collection = db.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(doc).iterator();

    ArrayList<HashMap<String, Object>> recordList = new ArrayList<>();

    try {

        while (cursor.hasNext()) {
            Document selectedDoc = cursor.next();
            recordList.add(convertDocumentToHashMap(selectedDoc));
        }

    } finally {

        cursor.close();
    }

    return recordList;
}