List of usage examples for com.mongodb DBCursor hasNext
@Override public boolean hasNext()
From source file:com.buzz.buzzdata.MongoBuzz.java
private String executeQueries(BasicDBObject[] queries, String combine, String collName) { String retval = ""; DBCollection buzzCollection = mongoDB.getCollection(collName); BasicDBObject filters = new BasicDBObject(combine, queries); DBCursor cursor = buzzCollection.find(filters); try {/*from w w w. jav a 2s . c o m*/ while (cursor.hasNext()) { retval += cursor.next(); retval += "\n"; } } finally { cursor.close(); } return retval; }
From source file:com.ca.apm.mongo.ShardCluster.java
License:Open Source License
private List<String> getShardsFromConfig(final String host, final int port) { final List<String> shardList = new ArrayList<String>(); MongoClient dbClient = null;/* www . ja va 2 s . c o m*/ try { dbClient = setupDbClient(host, port); final DB configDB = dbClient.getDB("config"); final DBCursor shardsCursor = configDB.getCollectionFromString("shards").find(); while (shardsCursor.hasNext()) { final DBObject dbo = shardsCursor.next(); String shards = (String) dbo.get("host"); String[] shardMembers = getShardMembers(shards); for (String member : shardMembers) { shardList.add(member); } } } catch (Exception e) { logger.log(Level.WARNING, "Exception getting shards from cfg servers: {0}", e); } finally { if (dbClient != null) { dbClient.close(); } } return shardList; }
From source file:com.ca.apm.mongo.ShardCluster.java
License:Open Source License
private List<String> getMongosFromConfig(final String host, final int port) { final List<String> shardRouters = new ArrayList<String>(); MongoClient dbClient = null;/*from w ww . j a v a 2 s.c o m*/ try { dbClient = setupDbClient(host, port); final DB configDB = dbClient.getDB("config"); final DBCursor mongosCursor = configDB.getCollectionFromString("mongos").find(); while (mongosCursor.hasNext()) { final DBObject dbo = mongosCursor.next(); final String mongos = (String) dbo.get("_id"); shardRouters.add(mongos); } } catch (Exception e) { logger.log(Level.WARNING, "Exception getting mongos from cfg server(s): {0}", e); } finally { if (dbClient != null) { dbClient.close(); } } return shardRouters; }
From source file:com.caci.dummyserver.MongoRepository.java
public GetObjectsResult getObjects(String table, Collection<Pair<String, String>> keys, Collection<String> fields, Collection<Pair<String, Collection<String>>> filters, Collection<String> sort, Integer offset, Integer limit) { DB db = mongo.getDB("Objects"); DBCollection col = db.getCollection(table); GetObjectsResult result = new GetObjectsResult(); DBObject queryObject = makeQueryObject(keys, filters); DBCursor cursor = col.find(queryObject); try {/*from ww w.j a v a 2 s . com*/ if (sort != null && !sort.isEmpty()) { DBObject sortObj = new BasicDBObject(); for (String fieldName : sort) { if (fieldName.startsWith("-")) { sortObj.put("data." + fieldName.substring(1), -1); } else { sortObj.put("data." + fieldName, 1); } } cursor.sort(sortObj); } result.setTotal(cursor.count()); if (offset != null && offset > 0) { cursor.skip(offset); } if (limit != null && limit > 0) { cursor.limit(limit); } while (cursor.hasNext()) { DBObject obj = cursor.next(); obj = pruneDBObject(getData(obj), fields); result.getResults().add(obj.toString()); } } finally { cursor.close(); } return result; }
From source file:com.callidusrobotics.droptables.model.DocumentDao.java
License:Open Source License
protected List<DBObject> getDocuments(DBCursor cursor) { List<DBObject> result = new LinkedList<DBObject>(); try {// w w w .j a v a 2 s .c o m while (cursor.hasNext()) { result.add(flatten(cursor.next())); } } finally { cursor.close(); } return result; }
From source file:com.card.loop.xyz.dao.LearningElementDAO.java
public ArrayList<DBObject> listAll(String collection) throws UnknownHostException { ArrayList<DBObject> list = new ArrayList<DBObject>(); Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port); DB db = mongo.getDB(AppConfig.DATABASE_LOOP); GridFS le_gfs = new GridFS(db, collection); DBCursor cursor = le_gfs.getFileList(); System.out.println(le_gfs.getFileList() + ""); while (cursor.hasNext()) { list.add(cursor.next());/*from ww w . ja v a2s . c om*/ } return list; }
From source file:com.ccoe.build.alerts.connector.Connector.java
License:Apache License
public static DBObject getLastRecord(DBCollection collection, Date startDate, Date endDate) { DBObject lastone = null;/*from ww w .j a va2s. c om*/ try { BasicDBObject searchQuery = new BasicDBObject(); QueryBuilder qb = new QueryBuilder(); qb.put("Date").greaterThanEquals(startDate).lessThanEquals(endDate); searchQuery.putAll(qb.get()); DBCursor cursor = collection.find(searchQuery); while (cursor.hasNext()) { lastone = cursor.next(); } } catch (MongoException e) { e.printStackTrace(); } return lastone; }
From source file:com.ccoe.build.alerts.connector.Connector.java
License:Apache License
public static double getMovingAverage(DBCollection collection, String field, Date startDate, Date endDate) { double weekSum = 0; DBObject record = null;//w ww. ja v a 2s .c o m int count = 0; try { BasicDBObject searchQuery = new BasicDBObject(); QueryBuilder qb = new QueryBuilder(); qb.put("Date").greaterThanEquals(startDate).lessThanEquals(endDate); searchQuery.putAll(qb.get()); DBCursor cursor = collection.find(searchQuery); while (cursor.hasNext()) { record = cursor.next(); DBObject dbo = (DBObject) record.get("Data"); Set<String> keySet = dbo.keySet(); for (String keyName : keySet) { if (field.equals(keyName)) { count++; Object keyValue = dbo.get(keyName); double keyValueNum = 0; if (keyValue instanceof Integer) { int intValue = (Integer) keyValue; keyValueNum = Double.parseDouble("" + intValue); } else if (keyValue instanceof Double) { keyValueNum = (Double) keyValue; } weekSum += keyValueNum; } } } } catch (MongoException e) { e.printStackTrace(); } DAYS = count; return weekSum * 1.0 / count; }
From source file:com.cedac.security.acls.mongo.MongoAclService.java
License:Apache License
@Override public List<ObjectIdentity> findChildren(ObjectIdentity parentIdentity) { LOG.debug(ACL, "Looking for children of object identity {}", parentIdentity); DBObject query = queryByParentIdentity(parentIdentity); DBObject projection = new BasicDBObject(objectIdFieldName, true); DBCursor cursor = null; try {//w ww . java 2 s . co m cursor = getAclCollection().find(query, projection); if (cursor.count() == 0) { LOG.debug(ACL, "No child object found for identity {}", parentIdentity); return null; } LOG.trace(ACL, "Streaming cursor in order to retrieve child object identities"); List<ObjectIdentity> oids = new ArrayList<ObjectIdentity>(); while (cursor.hasNext()) { oids.add(toObjectIdentity((DBObject) cursor.next().get(objectIdFieldName))); } return oids; } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.cedac.security.oauth2.provider.approval.MongoApprovalStore.java
License:Apache License
@Override public List<Approval> getApprovals(String userName, String clientId) { BasicDBObject query = new BasicDBObject(userIdFieldName, userName).append(clientIdFieldName, clientId); DBCursor cursor = null; try {/* w w w.j a va2 s. c o m*/ List<Approval> approvals = new ArrayList<Approval>(); cursor = getApprovalsCollection().find(query); while (cursor.hasNext()) { DBObject dbo = cursor.next(); approvals.add(new Approval((String) dbo.get(userIdFieldName), (String) dbo.get(clientIdFieldName), (String) dbo.get(scopeFieldName), (Date) dbo.get(expiresAtFieldName), Approval.ApprovalStatus.valueOf((String) dbo.get(statusFieldName)), (Date) dbo.get(lastModifiedAtFieldName))); } return approvals; } finally { if (cursor != null) { cursor.close(); } } }