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 
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM