List of usage examples for com.mongodb DBCursor count
public int count()
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public synchronized ArrayList<String> getChannelSettings() { DBCursor cur = channelSettings.find().sort(new BasicDBObject("name", 1)); ArrayList<String> res = new ArrayList<String>(cur.count() + 1); res.add("NO SEGMENTATION"); while (cur.hasNext()) { res.add(cur.next().get("name").toString()); }// ww w .j a v a 2 s . c o m cur.close(); return res; }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
private ArrayList<ObjectId> listIds(BasicDBObject query, DBCollection col) { DBCursor cursor = col.find(query); ArrayList<ObjectId> res = new ArrayList<ObjectId>(cursor.count()); while (cursor.hasNext()) { BasicDBObject o = (BasicDBObject) cursor.next(); res.add(o.getObjectId("_id")); }//from w ww. ja v a2s . c o m return res; }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public synchronized BasicDBObject[] getMeasurementStructure(ObjectId nucId, int[] structures, ArrayList<String> fields, boolean exactMatch) { BasicDBObject query = new BasicDBObject("nucleus_id", nucId); if (exactMatch) { query.append("structures", structures); DBObject res;/*from ww w. j a v a 2s . c o m*/ if (fields == null || fields.isEmpty()) { res = structureMeasurement.findOne(query); } else { BasicDBObject f = new BasicDBObject(); if (!fields.contains("structures")) { fields.add("structures"); } for (String s : fields) { f.append(s, 1); } res = structureMeasurement.findOne(query, f); } if (res == null) { return new BasicDBObject[] { query }; } else { return new BasicDBObject[] { (BasicDBObject) res }; } } else { query.append("structures", new BasicDBObject("$all", structures)); DBCursor cur; if (fields == null || fields.isEmpty()) { cur = structureMeasurement.find(query); } else { BasicDBObject f = new BasicDBObject(); if (!fields.contains("structures")) { fields.add("structures"); } for (String s : fields) { f.append(s, 1); } cur = structureMeasurement.find(query, f); } BasicDBObject[] res = new BasicDBObject[cur.count()]; int idx = 0; while (cur.hasNext()) { res[idx] = (BasicDBObject) cur.next(); idx++; } return res; } }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public void testDuplicateIds(MongoConnector other) { DBCursor cur = this.object3D.find(new BasicDBObject(), new BasicDBObject()); DBCursor cur2 = other.object3D.find(new BasicDBObject(), new BasicDBObject()); System.out.println("items:" + cur.count() + " items2:" + cur2.count()); int count = 0; while (cur.hasNext()) { DBObject o = cur.next();// w w w. j ava2s . c o m if (other.object3D.findOne(new BasicDBObject("_id", o.get("_id"))) != null) { count++; } } System.out.println("Duplicate:" + count); }
From source file:test.mongodb.servlet.MongoDBServlet.java
License:Open Source License
protected void handleQuery(PrintWriter pw, String i) { try {// ww w.j av a 2 s . c o m mongoDB.requestStart(); DBCollection coll = mongoDB.getCollection("testCollection"); BasicDBObject query = new BasicDBObject(); query.put("i", 71); DBCursor cur = coll.find(query); if (cur.count() == 1) pw.format("<h1>>testCollection.%s result: </h2><pre>%s</pre>\n", cur.next()); else pw.format("<h1>>testCollection.%s result</h2>No matches found!\n"); } finally { mongoDB.requestDone(); } }
From source file:xbdd.webapp.resource.feature.Report.java
License:Apache License
@SuppressWarnings("unchecked") @GET/* w ww . j av a 2 s .com*/ @Produces("application/json") public DBObject getSummaryOfAllReports(@Context final HttpServletRequest req) { final DB db = this.client.getDB("bdd"); final DBCollection collection = db.getCollection("summary"); final DBCollection usersCollection = db.getCollection("users"); final BasicDBObject user = new BasicDBObject(); user.put("user_id", req.getRemoteUser()); final DBCursor userCursor = usersCollection.find(user); DBObject userFavourites; if (userCursor.count() != 1) { userFavourites = new BasicDBObject(); } else { final DBObject uDoc = userCursor.next(); if (uDoc.containsField("favourites")) { userFavourites = (DBObject) uDoc.get("favourites"); } else { userFavourites = new BasicDBObject(); } } final DBCursor cursor = collection.find(); try { final BasicDBList returns = new BasicDBList(); DBObject doc; while (cursor.hasNext()) { doc = cursor.next(); final String product = ((String) ((DBObject) doc.get("coordinates")).get("product")); if (userFavourites.containsField(product)) { doc.put("favourite", userFavourites.get(product)); } else { doc.put("favourite", false); } returns.add(doc); } return returns; } finally { cursor.close(); } }