是 父评论里嵌套子评论数组 的形式![](https://img-blog.csdnimg.cn/b02b4fb7510649ce87f4ea1a1dfa2435.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Leq5rGC5oyH54K5,size_20,color_FFFFFF,t_70,g_se,x_16)
修改子评论
db.collection('Comments').where({
'_id': e.target.dataset.p_id,//父评论id
'records._id': e.target.dataset._id,//子评论id
}).update({
data: {
'records.$.content': "修改评论内容"
}
}).then(res => {
wx.showToast({
title: "修改成功"
})
})
删除子评论
db.collection('Comments').where({
'_id': p_id,//父评论id
}).update({
data: {
records: _.pull({
'_id': _id,//子评论id
})
}
}).then(res => {
wx.showToast({
title: "删除成功"
})
})
再嵌套一层数组
{
p_id:xxx,
"records":[
{
_id:yyy,
content:aaa,
"arr":[
{
"type": string
"area": number
"age": number
}
]
},
]
}
可用 elemMatch 匹配嵌套在对象数组里面的对象数组字段 places
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
records: _.pull({
arr: _.elemMatch({
area: 100,
age: 18,
})
})
}
})
参考文章
开发指引 /基础能力 /数据库 /增删查改 (SDK) /查询、更新数组/对象
开发者资源 /SDK 文档 /数据库 /Command /更新·数组操作符 /pull
更新集合下的数组中的某一个值
https://developers.weixin.qq.com/community/develop/doc/000e0c11d1c2e843034a0fa7e5b800