像大多数数据库产品一样,MongoDB 也是用游标来循环处理每一条结果数据,具体语法如
下:
- >for(varc=db.t3.find();c.hasNext();){
- printjson(c.next());
- }
- {"_id":ObjectId("4fb8e4838b2cb86417c9423a"),"age":1}
- {"_id":ObjectId("4fb8e4878b2cb86417c9423b"),"age":2}
- {"_id":ObjectId("4fb8e4898b2cb86417c9423c"),"age":3}
- {"_id":ObjectId("4fb8e48c8b2cb86417c9423d"),"age":4}
- {"_id":ObjectId("4fb8e48e8b2cb86417c9423e"),"age":5}
MongoDB 还有另一种方式来处理游标
- >db.t3.find().forEach(function(u){printjson(u);});
- {"_id":ObjectId("4fb8e4838b2cb86417c9423a"),"age":1}
- {"_id":ObjectId("4fb8e4878b2cb86417c9423b"),"age":2}
- {"_id":ObjectId("4fb8e4898b2cb86417c9423c"),"age":3}
- {"_id":ObjectId("4fb8e48c8b2cb86417c9423d"),"age":4}
- {"_id":ObjectId("4fb8e48e8b2cb86417c9423e"),"age":5}
- >