mongodb 批量修改

db.getCollection('11111').find({}).forEach(
    function(item) {
        item.date =  item.date.replace("2018-11-23","2018-11-24");
        db.getCollection('11111').save(item);
    }
)
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);        
                      })
                    }              
            }
        }
    )
1、下面集合中的seq类型为double,想修改为int。

{
    "_id" : ObjectId("592e94fee820cc1813f0b9a2"),
    "privateAttrs" : [
        {
            "attrId" : "100",
            "seq" : 0
        },
        {
            "attrId" : "101",
            "seq" : 1
        }
    ]
}
{
    "_id" : ObjectId("592e94fee820cc1813f0b9a3"),
    "privateAttrs" : [
        {
            "attrId" : "102",
            "seq" : 0
        }
    ]
}
2、通过forEach遍历数组:
db.category.find().forEach(
    function(item) {
        item.privateAttrs.forEach(
            function(arr, index) {
                item.privateAttrs[index].seq = new NumberInt(item.privateAttrs[index].seq);
            }
        );
        db.category.save(item);
    }
)
3、forEach 说明
forEach方法中的function回调有三个参数: 第一个参数是遍历的数组内容,第二个参数是对应的数组索引,第三个参数是数组本身
--------------------- 
作者:__時__ 
来源:CSDN 
原文:https://blog.csdn.net/jsdxshi/article/details/72841283 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/yaowen/p/10082943.html