The way written in the cloud development document is to write the field name 'array.0' if the first one is updated. Such as:
But I want to update the key attribute of the i-th element of an array (stored in an object), i is a parameter, and I currently use the template string `array.${i}.key` and the string splicing 'array .'+i+'.key', neither of these methods will work.
After seeing the first solution in this tutorial , I found a solution!
Use the $ positional placeholder:
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)
}
})
}
where can not only find documents, but also find the position of eligible elements in an array in the document!
Then use the $ symbol that stores the position data to update the properties of the i-th element in the array.
My original answer: How does the cloud function update the specified array element? - WeChat open community https://developers.weixin.qq.com/community/develop/doc/0000e479ca4890c7be1941bbc5b800