List of usage examples for com.mongodb DBCollection find
public DBCursor find(final DBObject query)
From source file:com.aw.app.action.TroopsAction.java
/** * /*from w w w . j av a 2 s . co m*/ * @param uid * @return */ public long getRemainingTroopSpace(long uid) { long totalSpace = new BuildingAction().getTotalHousingSpace(uid); long consumedSpace = 0; long remainingSpace = totalSpace; DBCollection table = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user_troops"); BasicDBObject query = new BasicDBObject("uid", uid); DBCursor cursor = table.find(query); Map troopMap = new HashMap(); while (cursor.hasNext()) { DBObject userTroop = cursor.next(); long troopId = (Long) userTroop.get("troop_id"); if (troopMap.containsKey(troopId)) { long value = (Long) troopMap.get(troopId); ++value; troopMap.put(troopId, value); } else { troopMap.put(troopId, 1); } } Iterator entries = troopMap.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); long key = (Long) entry.getKey(); long value = (Long) entry.getValue(); DBCollection configTable = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_troops_configuration"); DBCursor configCursor = configTable.find(new BasicDBObject("troop_id", key)); while (configCursor.hasNext()) { DBObject config = configCursor.next(); long configHousingSpace = (Long) config.get("housing_space"); consumedSpace = consumedSpace + (configHousingSpace * value); } } remainingSpace = remainingSpace - consumedSpace; if (remainingSpace < 0) remainingSpace = 0; return remainingSpace; }
From source file:com.aw.services.AncientWarServiceImpl.java
/** * /*from w ww.ja v a 2 s . c o m*/ * @param userId * @return * @throws JSONException */ @Override public JSONObject browseUserVillage(Long userId) throws JSONException { DBCollection coll = MongoDbUtil.getCollection("ancient_war", "aw_user"); DBObject query = QueryBuilder.start("uid").is(userId).get(); DBCursor cursor = coll.find(query); JSONObject status = new JSONObject(); while (cursor.hasNext()) { DBObject user = cursor.next(); if (user != null) { Long uid = (Long) user.get("uid"); JSONObject userVillage = getUserVillage(uid, "visit"); return userVillage; } else { status.put("status", false); status.put("message", "No Village found"); return status; } } return status; }
From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java
License:Apache License
/** * Gets chunks./*from w ww .jav a 2s . c o m*/ * * @param collection the collection * @return the chunks */ private DBCursor getChunks(DBCollection collection) { DB config = collection.getDB().getSisterDB("config"); DBCollection configChunks = config.getCollection("chunks"); return configChunks.find(new BasicDBObject("ns", collection.getFullName())); }
From source file:com.buzz.buzzdata.MongoBuzz.java
private String getBuzz(BasicDBObject query, Double lat, Double lng) { String retval = ""; DBCollection buzzCollection = mongoDB.getCollection("BuzzInfo"); DBObject sort = new BasicDBObject(); sort.put("modified", -1); DBCursor cursor = buzzCollection.find(query).sort(sort); try {/*ww w.j ava 2s. com*/ while (cursor.hasNext()) { //get buzzid DBObject buzz_obj = cursor.next(); ObjectId buzz_id = (ObjectId) buzz_obj.get("_id"); //get images for buzzid GridFS gridFS = new GridFS(mongoDB); BasicDBObject check_images = new BasicDBObject("BuzzID", buzz_id); List<GridFSDBFile> dbfiles = gridFS.find(check_images); String image_links = ""; for (GridFSDBFile file : dbfiles) { String _buzz_id = buzz_id.toString(); String pic_num = file.get("PicNum").toString(); if (!image_links.equals("")) { image_links += ",http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid=" + _buzz_id + "&pic_num=" + pic_num; } else { image_links += "http://192.168.0.11:8080/BuzzRestAPI/webresources/buzz/Image?buzzid=" + _buzz_id + "&pic_num=" + pic_num; } } String imgs = "\"Images\": " + "\"" + image_links + "\""; retval += buzz_obj; retval = retval.substring(0, retval.length() - 1); retval += ", " + imgs; double lat2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(0); double lng2 = (double) ((BasicDBList) buzz_obj.get("loc")).get(1); double dist = this.haversine(lat, lng, lat2, lng2); retval += ", \"Distance\": " + dist; String directions_url = "https://maps.google.com/maps?saddr=" + lat + "," + lng + "&daddr=" + lat2 + "," + lng2 + "&hl=en&sll=" + lat + "," + lng + "&sspn=" + lat2 + "," + lng2 + "&t=m&mra=mift&mrsp=1&sz=5&z=18"; retval += ", \"Directions\": \"" + directions_url + "\""; retval += "},"; } } catch (Exception exc) { } finally { cursor.close(); } retval = retval.substring(0, retval.length() - 1); retval = "callback({\"Buzzes\": [" + retval + "]})"; return retval; }
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 .j a v a 2 s .co m*/ while (cursor.hasNext()) { retval += cursor.next(); retval += "\n"; } } finally { cursor.close(); } return retval; }
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 {// w w w . ja v a 2s . co m 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
/** * Fetches a document from a collection. * * @param collectionName/*w w w . j a va 2 s . c om*/ * The collection containing the document. * @param id * The document ID * @return The document, nullable */ public DBObject getDocument(String collectionName, ObjectId id) { DBCollection collection = getCollection(collectionName); DBCursor cursor = collection.find(makeRef(id)); List<DBObject> results = getDocuments(cursor); if (results.size() > 0) { return results.get(0); } return null; }
From source file:com.callidusrobotics.droptables.model.DocumentDao.java
License:Open Source License
/** * Finds documents containing the desired key,value pair. * * @param collectionName/*w ww. j av a 2s.c o m*/ * The collection to search * @param key * The document key to select on * @param value * The desired value * @return A list of matching documents, never null */ public List<DBObject> getDocumentsByField(String collectionName, String key, String value) { DBCollection collection = getCollection(collectionName); DBObject query = QueryBuilder.start().and(key).is(value).get(); DBCursor cursor = collection.find(query); return getDocuments(cursor); }
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 w ww . j a v a 2 s .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;/*from www . j av a2s .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; }