Example usage for com.mongodb.client MongoCursor next

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

Introduction

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

Prototype

@Override
    TResult next();

Source Link

Usage

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

public long[] SelectList(String table, String key, BsonDocument where) {
    key = key.toLowerCase();/*  w  ww .j  av a  2 s  .co 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)).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();/* w ww  .  j  ava 2s .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)).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 String ReadAsSDF(String table, String molfilekey) {
    MongoCollection coll = db.getCollection(table);
    FindIterable iter = coll.find();/*  w  w  w .java2s .  com*/

    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

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

    try {/*  ww w.  j  ava  2  s . c om*/
        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   w  w  w.  ja v a2 s  .  c om
    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.
 * /*from   w ww .j a  va2  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<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  .ja va  2  s .co m*/
    }

    cursor.close();
}

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

License:Open Source License

/**
 * API for selecting records from DB table.
 * // www .j av  a  2  s .co  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<>();

    try {

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

    } finally {

        cursor.close();
    }

    return recordList;
}

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

License:Open Source License

/**
 * API for finding resources matched filterValue of filterKey in collection
 *
 * @param filterKey// w w  w. java2 s  .  co m
 *            field name in collection
 * @param filterValue
 *            field value about field name
 * @param tablename
 *            collection name
 * @return ArrayList<PublishPayloadFormat> - array list of resource
 *         information
 */
public ArrayList<PublishPayloadFormat> readResource(String filterKey, String filterValue, String tablename) {
    MongoCollection<Document> collection = db.getCollection(tablename);
    ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
    MongoCursor<Document> cursor = collection.find(Filters.eq(filterKey, filterValue)).iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            resourceFormatList.add(convertDocumentToResourceFormat(doc));
        }
    } finally {
        cursor.close();
    }

    return resourceFormatList;
}

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

License:Open Source License

/**
 * API for finding resources matched filterValue of filterKey and a
 * particular device ID in collection//  w  w w  .  ja v  a  2  s .c o  m
 *
 * @param di
 *            device id
 * @param filterKey
 *            field name in collection
 * @param filterValue
 *            field value about field name
 * @param tablename
 *            collection name
 * @return ArrayList<PublishPayloadFormat> - array list of resource
 *         information
 */
public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di, String filterKey, String filterValue,
        String tablename) {
    MongoCollection<Document> collection = db.getCollection(tablename);
    ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
    MongoCursor<Document> cursor = collection
            .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di), Filters.eq(filterKey, filterValue)))
            .iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            resourceFormatList.add(convertDocumentToResourceFormat(doc));
        }
    } finally {
        cursor.close();
    }

    return resourceFormatList;
}