游标

  find方法使用游标返回查询结果,游标的客户端实现使你可以对最终结果做很多的控制。在shell里创建一个游标很简单,往collection里放些document,

  执行查询,将返回结果指派给一个本地变量即可。

> var cursor = db.collection.find();

> while (cursor.hasNext()) {

   obj = cursor.next();
   }

  cursor类同样实现了iterator接口,所以你可以使用forEach循环

> var cursor = db.people.find();
> cursor.forEach( function (x) {
... print(x.name);
... });

db.Goods.find().forEach(
    function(item){
        if(!item.goodsCode.indexOf("ABCD")){
                var tempGoodId=item._id;
                var tempGoodCode=item.goodsCode;
                var temp=db.Goods.findOne({"goodsCode":{"$regex":"^"+tempGoodCode+".+"}});
                if(temp){
                    // print(tempGoodCode+"="+item._id);
                    var cursor=db.GoodAttr.find({"goodsId":tempGoodId});
                     cursor.forEach(function(a){
                        print(a);        
                      })
                    }              
            }
        }
    )


Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐