List of usage examples for com.mongodb.client MongoCursor next
@Override TResult next();
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);/*w w w . j a v a2 s. c o m*/ 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 www . j a va 2 s . c o m*/ 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 w w. j a va2 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 w w w . j a v a 2 s .co m }
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 {//w w w . java2 s .co 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; }/* w w w. jav a 2 s .c o 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 av a 2s . c o m } 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; }// ww w . ja v a 2s . 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); }
From source file:org.opencb.commons.datastore.mongodb.MongoDBCollection.java
License:Apache License
private <T> QueryResult<T> privateFind(Bson query, Bson projection, Class<T> clazz, ComplexTypeConverter<T, Document> converter, QueryOptions options) { long start = startQuery(); /**//w w w . j a va 2 s .c om * Getting the cursor and setting the batchSize from options. Default value set to 20. */ FindIterable<Document> findIterable = mongoDBNativeQuery.find(query, projection, options); MongoCursor<Document> cursor = findIterable.iterator(); QueryResult<T> queryResult; List<T> list = new LinkedList<>(); if (cursor != null) { if (queryResultWriter != null) { try { queryResultWriter.open(); while (cursor.hasNext()) { queryResultWriter.write(cursor.next()); } queryResultWriter.close(); } catch (IOException e) { cursor.close(); queryResult = endQuery(null, start); queryResult.setErrorMsg(e.getMessage() + " " + Arrays.toString(e.getStackTrace())); return queryResult; } } else { if (converter != null) { while (cursor.hasNext()) { list.add(converter.convertToDataModelType(cursor.next())); } } else { if (clazz != null && !clazz.equals(Document.class)) { Document document; while (cursor.hasNext()) { document = cursor.next(); try { list.add(objectMapper.readValue(objectWriter.writeValueAsString(document), clazz)); } catch (IOException e) { e.printStackTrace(); } } } else { while (cursor.hasNext()) { list.add((T) cursor.next()); } } } } if (options != null && options.getInt(QueryOptions.SKIP) <= 0 && options.getInt(QueryOptions.LIMIT) > 0) { int numTotalResults; if (options.getBoolean(QueryOptions.SKIP_COUNT)) { numTotalResults = -1; } else { try { // numTotalResults = findIterable.maxTime(options.getInt("countTimeout"), TimeUnit.MILLISECONDS).count(); numTotalResults = (int) mongoDBNativeQuery.count(query); } catch (MongoExecutionTimeoutException e) { numTotalResults = -1; } } queryResult = endQuery(list, numTotalResults, start); } else { queryResult = endQuery(list, start); } cursor.close(); } else { queryResult = endQuery(list, start); } return queryResult; }
From source file:org.opencb.commons.datastore.mongodb.MongoDBCollection.java
License:Apache License
public <T> QueryResult<T> aggregate(List<? extends Bson> operations, ComplexTypeConverter<T, Document> converter, QueryOptions options) { long start = startQuery(); QueryResult<T> queryResult;// w w w .ja va 2 s . c om AggregateIterable<Document> output = mongoDBNativeQuery.aggregate(operations, options); MongoCursor<Document> iterator = output.iterator(); List<T> list = new LinkedList<>(); if (queryResultWriter != null) { try { queryResultWriter.open(); while (iterator.hasNext()) { queryResultWriter.write(iterator.next()); } queryResultWriter.close(); } catch (IOException e) { queryResult = endQuery(list, start); queryResult.setErrorMsg(e.getMessage() + " " + Arrays.toString(e.getStackTrace())); return queryResult; } } else { if (converter != null) { while (iterator.hasNext()) { list.add(converter.convertToDataModelType(iterator.next())); } } else { while (iterator.hasNext()) { list.add((T) iterator.next()); } } } queryResult = endQuery(list, start); return queryResult; }