BasicDBObject dbo = new BasicDBObject(); dbo.put("name", "first"); collection.insert(dbo); dbo.put("_id", null); dbo.put("name", "second"); dbo.put("otherInfo", new BasicDBObject("text", "sometext")); collection.insert(dbo); DBObject query = new BasicDBObject("otherInfo.text", new BasicDBObject("$exists", true)); DBCursor result = collection.find(query); System.out.println(result.size()); System.out.println(result.iterator().next());
@Override public Iterator<DBObject> iterator() { return cursor.iterator(); }
@Override public Iterator<DBObject> iterator() { return cursor.iterator(); }
@Override public Iterator<Row<T, K>> iterator() { final Iterator<DBObject> iterator = dbCursor.iterator(); return new Iterator<Row<T, K>>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Row<T, K> next() { DBObject next = iterator.next(); return newrow((K) next.get("id"), next); } @Override public void remove() { } }; } };
@Autowired private MongoTemplate mongoTemplate; DBObject query = new BasicDBObject("field", "value"); DBCursor dbCursor = mongoTemplate.getCollection("collectionName").find(query); Iterator<DBObject> iterator = dbCursor.iterator(); while(iterator.hasNext()){ Object value = iterator.next().get("otherfield"); }
public Iterator<DBObject> find(final DBObject dbObject) { connect(); return coll.find(dbObject).iterator(); }
@Override public ImmutableList<ObjectId> getChildren(ObjectId id) { DBObject query = new BasicDBObject(); query.put("_label", Relationship.PARENT.name()); query.put("_out", id.toString()); DBCursor cursor = collection.find(query); Function<DBObject, ObjectId> idMapper = new Function<DBObject, ObjectId>() { @Override public ObjectId apply(DBObject o) { return ObjectId.valueOf((String)o.get("_in")); } }; return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper)); }
@Override public ImmutableList<ObjectId> getParents(ObjectId id) { DBObject query = new BasicDBObject(); query.put("_label", Relationship.PARENT.name()); query.put("_in", id.toString()); DBCursor cursor = collection.find(query); Function<DBObject, ObjectId> idMapper = new Function<DBObject, ObjectId>() { @Override public ObjectId apply(DBObject o) { return ObjectId.valueOf((String)o.get("_out")); } }; return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper)); }
public Iterator<DBObject> find(final String json) { connect(); return coll.find((DBObject) JSON.parse(json)).iterator(); }
currentCursorIterator = result.cursor.iterator(); while (currentCursorIterator.hasNext())
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, DBObject query) { MapReduceOutput output = getCollection().mapReduce(map, reduce, outputTarget, outputType, query); DBCollection c = output.getOutputCollection(); DBCursor cursor; if(orderBy != null){ cursor = c.find().sort(SortUtil.getSort(orderBy)); }else{ cursor = c.find(); } List<DBObject> list = new ArrayList<>(); for(Iterator<DBObject> it = cursor.iterator(); it.hasNext(); ){ list.add(it.next()); } return list; }
while (noNextResult) currentCursorIterator = result.cursor.iterator(); if (currentCursorIterator.hasNext())
return Iterators.transform(cursor.iterator(), mapper);
Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("test");// Use DB DBObject allQuery = new BasicDBObject(); DBCollection collection = db.getCollection("dimensions"); DBCursor curs = collection.find(); Iterator<DBObject> fields = curs.iterator(); while(fields.hasNext()){ // BasicDBList geoList = (BasicDBList) fields.next().get("GeoLevels"); BasicDBObject object = (BasicDBObject) geoList.get(0); // this should return {"5": "Continent_Name"} Object value = object.get("5); // value should contain "Continent_Name" }
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query) { MapReduceOutput output = getCollection().mapReduce(map, reduce, outputTarget, outputType, query); DBCollection c = output.getOutputCollection(); DBCursor cursor; if(orderBy != null){ cursor = c.find().sort(SortUtil.getSort(orderBy)).skip((pageNum-1)*pageSize).limit(pageSize); }else{ cursor = c.find().skip((pageNum-1)*pageSize).limit(pageSize); } List<DBObject> list = new ArrayList<>(); for(Iterator<DBObject> it = cursor.iterator(); it.hasNext(); ){ list.add(it.next()); } return list; }
public Iterable<DBObject> mapReduce(String collectionName, String map, String reduce, String outputTarget, OutputType type, OrderBy order, int pageNumber, int pageSize, DBObject query) throws OperatorException { MapReduceOutput _output = getCollection(collectionName).mapReduce(map, reduce, outputTarget, type, query); CommandResult _result = _output.getCommandResult(); if (!_result.ok()) { throw new OperatorException(_result.getErrorMessage()); } DBCollection _collection = _output.getOutputCollection(); DBCursor _cursor = null; if (order != null) { _cursor = _collection.find().sort(order.toDBObject()); } else { _cursor = _collection.find(); } if (pageNumber > 0 && pageSize > 0) { _cursor.skip((pageNumber - 1) * pageSize).limit(pageSize); } List<DBObject> _results = new ArrayList<DBObject>(); for (Iterator<DBObject> _it = _cursor.iterator(); _it.hasNext();) { _results.add(_it.next()); } return _results; }