1***@163.com
1***@163.com
  • 发布:2024-02-25 22:18
  • 更新:2024-02-26 09:06
  • 阅读:112

如何批量更新嵌套数组

分类:uniCloud

在嵌套数组中,如何把表里边所有“标签2”改为“新标签”,查询如下
let res = await db.collection('table').where({
'goods.category': "标签2"
}).get()

如果对应标签2位置固定在0上的可以这么更新
let res = await db.collection('table').where({
'goods.category': "标签2"
}).update({
'goods.$.category.0': "新标签"
})

但是如果位置不固定的时候,有没有办法批量更新


// 更新前  
[{  
    "_id": "1",  
    "goods": [{  
        name:"物品1",  
        category: ["标签1","标签2","标签3"]  
    },{  
        name:"物品2",  
        category: ["标签2","标签4","标签5"]  
    }]  
},{  
    "_id": "2",  
    "goods": [{  
        name:"物品3",  
        category: ["标签2","标签3","标签5"]  
    },{  
        name:"物品4",  
        category: ["标签1","标签2","标签4"]  
    }]  
}]  

// 更新后  
[{  
    "_id": "1",  
    "goods": [{  
        name:"物品1",  
        category: ["标签1","新标签","标签3"]  
    },{  
        name:"物品2",  
        category: ["新标签","标签4","标签5"]  
    }]  
},{  
    "_id": "2",  
    "goods": [{  
        name:"物品3",  
        category: ["新标签","标签3","标签5"]  
    },{  
        name:"物品4",  
        category: ["标签1","新标签","标签4"]  
    }]  
}]
2024-02-25 22:18 负责人:无 分享
已邀请:
喜欢技术的前端

喜欢技术的前端 - QQ---445849201

this.arr.forEach((item) => {  
                    item.goods.forEach((good) => {  
                        good.category = good.category.map((tag) => (tag === "标签2" ? "新标签" : tag));  
                    });  
                });
  • 1***@163.com (作者)

    感谢,我完善了下问题描述,是关于云函数操作数据库的问题

    2024-02-26 10:09

要回复问题请先登录注册