微信小程序-云数据库-嵌套数组的修改删除操作

是 父评论里嵌套子评论数组 的形式《微信小程序-云数据库-嵌套数组的修改删除操作》

修改子评论

      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

删除
https://developers.weixin.qq.com/community/develop/doc/0000202fad8c38ed3a292862f51800?_at=1569973995514

    原文作者:跪求指点
    原文地址: https://blog.csdn.net/tfnmdmx/article/details/123875947
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞