云数据库嵌套数组元素增/删(3+层嵌套)?
发布于 6 年前 作者 junsong 509 次浏览 来自 官方Issues

请教

我的集合如下结构,数组嵌套了 3 层(我的集合有多条记录),现在我需要对第 3 层的数组进行 push、pull 操作,请问代码我该怎么写(目前云开发数据库跟 MongoDB 还是有一定上的差异)?

{
  _id: 'serial_id',
  nodes: [
    _id: 'serial_id_a',
    text: 'label',

    nodes: [
      _id: 'serial_id_b',
      text: 'label',

      nodes: [
        _id: 'serial_id_c',
        text: 'label'
      ]
    ]
  ]
}

 

1 回复

可以看看这个文档https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/query-array-object.html

大概意思是下面这样,只能用下标定位

db.collection('xxx').doc('serial_id').update({
  data: {
    'nodes.0.nodes.0.nodes': _.push([{'_id':'xx', 'text':'xx'}])
  },
})
回到顶部