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.iotivity.cloud.rdserver.MongoDB.java

License:Open Source License

/**
 * API for finding resources matched filterValue of filterKey in collection
 *
 * @param filterKey/* w w  w.j a  v  a  2s  .  c o 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/*from   w w w  . j a va  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;
}

From source file:org.iu.sead.cloud.ROSearch.java

License:Apache License

private Response getAllPublishedROs(Document filter, Date start, Date end, String creatorRegex) {
    FindIterable<Document> iter = publicationsCollection.find(filter);
    setROProjection(iter);// ww w.j  av a2s  .com
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        Document document = cursor.next();
        reArrangeDocument(document);
        if (withinDateRange(document.getString("Publication Date"), start, end)
                && creatorMatch(document.get("Creator"), creatorRegex)) {
            array.put(JSON.parse(document.toJson()));
        }
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.iu.sead.cloud.ROSearch.java

License:Apache License

private Response getMostDownloadedROs(Document filter) {
    FindIterable<Document> iter = publicationsCollection.find(filter);
    setROProjection(iter);//from   w  w  w .j  av a 2s . c  om
    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        Document document = cursor.next();
        Document agg = (Document) document.get("Aggregation");
        int downloads = SeadMon
                .queryLandingPageLogs(MonConstants.EventType.DOWNLOAD, agg.getString("Identifier"), null, null)
                .size();
        if (downloads == 0) {
            downloads = SeadMon
                    .queryLandingPageLogs(MonConstants.EventType.DOWNLOAD, agg.getString("Title"), null, null)
                    .size();
        }
        reArrangeDocument(document);
        document.append("Downloads", "" + downloads);
        array.put(JSON.parse(document.toJson()));
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:org.jooby.mongodb.MongoSessionStore.java

License:Apache License

private boolean existsIdx(final String name) {
    MongoCursor<Document> iterator = sessions.listIndexes().iterator();
    while (iterator.hasNext()) {
        Document doc = iterator.next();
        if (doc.getString("name").equals(name)) {
            return true;
        }/*  w ww.ja  v a  2 s. co  m*/
    }
    return false;
}

From source file:org.netbeans.modules.mongodb.api.MongoCursorResult.java

License:Open Source License

public void setCursor(MongoCursor<BsonDocument> cursor) {
    documents = new ArrayList<>();
    while (cursor.hasNext() && documents.size() < maxSize) {
        documents.add(cursor.next());/*from ww w  .j  a va 2  s. c  om*/
    }
}

From source file:org.opencb.cellbase.lib.db.MongoDBAdaptor.java

License:Apache License

protected List<QueryResult> executeQueryList2(List<? extends Object> ids, List<Document> queries,
        QueryOptions options, MongoDBCollection mongoDBCollection2) {
    List<QueryResult> queryResults = new ArrayList<>(ids.size());
    long dbTimeStart, dbTimeEnd;

    for (int i = 0; i < queries.size(); i++) {
        Document query = queries.get(i);
        QueryResult queryResult = new QueryResult();
        queryResult.setId(ids.get(i).toString());

        // Execute query and calculate time
        dbTimeStart = System.currentTimeMillis();
        if (options.containsKey("count") && options.getBoolean("count")) {
            queryResult = mongoDBCollection2.count(query);
        } else {//from  w  w  w  .ja v a  2s. c  o  m
            MongoCursor<Document> cursor = mongoDBCollection2.nativeQuery().find(query, options).iterator();
            List<Document> dbObjectList = new LinkedList<>();
            while (cursor.hasNext()) {
                dbObjectList.add(cursor.next());
            }
            queryResult.setNumResults(dbObjectList.size());
            queryResult.setResult(dbObjectList);

            // Limit is set in queryOptions, count number of total results
            if (options != null && options.getInt("limit", 0) > 0) {
                queryResult.setNumTotalResults(mongoDBCollection2.count(query).first());
            } else {
                queryResult.setNumTotalResults(dbObjectList.size());
            }
        }
        dbTimeEnd = System.currentTimeMillis();
        queryResult.setDbTime(Long.valueOf(dbTimeEnd - dbTimeStart).intValue());

        queryResults.add(queryResult);
    }

    return queryResults;
}

From source file:org.opencb.commons.datastore.mongodb.MongoDataStoreManager.java

License:Apache License

public boolean exists(String database) {

    if (database != null && !database.trim().equals("")) {
        try (MongoClient mc = newMongoClient()) {
            MongoCursor<String> dbsCursor = mc.listDatabaseNames().iterator();
            while (dbsCursor.hasNext()) {
                if (dbsCursor.next().equals(database)) {
                    return true;
                }/*from   www .j  a  v  a 2s  .  co m*/
            }
        }
    }
    return false;
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBCollection.java

License:Apache License

public QueryResult<String> distinct(String key, Bson query) {
    long start = startQuery();
    List<String> l = new ArrayList<>();
    MongoCursor<BsonValue> iterator = mongoDBNativeQuery.distinct(key, query, BsonValue.class).iterator();
    while (iterator.hasNext()) {
        BsonValue value = iterator.next();
        if (value == null || value.isNull()) {
            l.add(null);/*from  w w  w.j  a v a 2  s  .c om*/
        } else if (value.isString()) {
            l.add(value.asString().getValue());
        } else {
            throw new IllegalArgumentException(
                    "Found result with BsonType != " + BsonType.STRING + " : " + value.getBsonType());
        }
    }
    return endQuery(l, start);
}

From source file:org.opencb.commons.datastore.mongodb.MongoDBCollection.java

License:Apache License

public <T> QueryResult<T> distinct(String key, Bson query, Class<T> clazz) {
    if (clazz == null || clazz.equals(String.class)) {
        QueryResult<T> result = (QueryResult<T>) distinct(key, query);
        result.setResultType(String.class.getName());
        return result;
    }/*from  www.  j a  v a  2 s. c o  m*/
    long start = startQuery();
    List<T> list = new ArrayList<>();
    MongoCursor iterator = mongoDBNativeQuery.distinct(key, query, clazz).iterator();
    while (iterator.hasNext()) {
        Object next = iterator.next();
        if (next != null) {
            list.add((T) next);
        }
    }
    return endQuery(list, start);
}