mongodb 批量修改

本文详细介绍了如何使用MongoDB进行数据操作,包括日期字段的批量修改,特定字符串匹配查询,以及数组中double类型转换为int类型的实现方法。同时,讲解了forEach方法在数组遍历中的应用及参数说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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回调有三个参数: 第一个参数是遍历的数组内容,第二个参数是对应的数组索引,第三个参数是数组本身
--------------------- 
作者:__時__ 
来源:优快云 
原文:https://blog.youkuaiyun.com/jsdxshi/article/details/72841283 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

转载于:https://www.cnblogs.com/yaowen/p/10082943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值