云函数如何更新指定的数组元素?
发布于 5 年前 作者 min56 11432 次浏览 来自 官方Issues

文档里写的方式是如果更新第1个就写字段名为’array.0’,但是我想更新一个数组的第i个元素,i为参数,我目前使用了模版字符串`array.${i}`和字符串拼接‘array.’+i,这两种方法都不行,请问有什么解决办法吗?谢谢!

2 回复

在此之前都是用循环处理的,贼慢,新方法不错

找到解决办法了!用$位置占位符

https://developers.weixin.qq.com/community/develop/article/doc/00022a7c234cd8865238c81ab5bc13 的第一种办法想到的解决思路:

exports.main = async(event, context) => {
    return db.collection('user').where({
        '_id':event.docID,
        'array.key': event.key
    }).update({
        data: {
            'array.$.key': event.value,
            arrNum: _.inc(1)
        },
        success: res => {
            console.log(res)
        }
    })
}
回到顶部