MongoDB高级查询 互动版

forEach遍历游标

MongoDB 还有另一种方式来处理游标,即forEach()方法:

> 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 }