• 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 
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    [洛谷P1886]滑动窗口 (单调队列)(线段树)
    树状数组详细解析
    离散化
    kettle使用总结(一)
    springBoot开发的web项目打war包部署到已有的tomcat容器中
    集群时钟同步
    linux服务器安装nginx
    Tomcat学习之体系架构
    码云新建仓库上传项目
    linux系统安装mysql yum方式
  • 原文地址:https://www.cnblogs.com/yaowen/p/10082943.html
Copyright © 2020-2023  润新知