@Test public void testBasicQuery() { BasicDBObject query = new BasicDBObject(); query.put(EmployeeProperties.EMPLOYEE_ID, "28241"); BasicDBObject fields = new BasicDBObject(); fields.put(EmployeeProperties.ID, 0); fields.put(EmployeeProperties.LAST_NAME, 1); fields.put(EmployeeProperties.EMPLOYEE_ID, 1); DBCollection collection = mongoOps.getCollection(EmployeeProperties.COLLECTION); log.info( "MongoDB Query Explain Plan: " + collection .find(query, fields) .hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1)) .explain()); DBCursor cursor = collection.find(query, fields).hint(new BasicDBObject(EmployeeProperties.EMPLOYEE_ID, 1)); log.info(cursor.count() + ""); assertNotNull("cursor was null.", cursor); assertEquals("cursor count was not 1.", 1, cursor.count()); log.info("Query (" + query.toString() + ")"); while (cursor.hasNext()) { DBObject dbo = cursor.next(); log.info(dbo.toString()); assertEquals("Keyset size not equal to 2.", 2, dbo.keySet().size()); } log.info(cursor.explain().toString()); }
public <T> T eachNode(ReadChildrenEach<T> readJob) { DBCursor cursor = null; try { cursor = collection.find(filters(), fields(), skip, offset).sort(orderBy).limit(offset); if (this.hint != null) cursor.hint(hint); else if (this.hintIndexName != null) cursor.hint(hintIndexName); session.attribute(Explain.class.getCanonicalName(), Explain.create(cursor.explain())); ReadChildrenIterator citer = ReadChildrenIterator.create(session, cursor); T result = readJob.handle(citer); return result; } finally { IOUtil.close(cursor); } }