【问题标题】:MongoDB - Project a nested object of of a recursive document structureMongoDB - 投影递归文档结构的嵌套对象
【发布时间】:2018-02-26 14:08:19
【问题描述】:

经过几天的研究,我需要一些帮助。我想从我的递归结构化对象中找到一个嵌套对象:

{
  id: "1",
  model: {
    dataList: [
      {
        name: 'someName',
        semantic: ['unique','string','array']
        bitOffset: 1,
        bitLength: 16,
        subDataList:[
          {
            name: 'someName',
            semantic: ['also','unique','array']
            bitOffset: 1,
            bitLength: 16,
            subDataList:[
              {
                name: 'someName',
                semantic: ['even','more','unique']
                bitOffset: 1,
                bitLength: 16,
                subDataList:[...]
              },
              {...}
            ]
          },
          {...}
        ]
      },
      {...}
    ]
  }
}

我想从查询中获取其中一个子文档。比如这个:

   {
     name: 'someName',
     semantic: ['even','more','unique']
     bitOffset: 1,
     bitLength: 16,
     subDataList:[...]
   }

我不想得到所有的父母。只有内部对象及其子对象。我尝试了多种查询形式,包括$where$unwind§replaceRoot...,但我无法收到所需的嵌套文档。

嵌套文档的标识符是语义数组。数组元素(总是字符串)的组合是唯一的。

关于如何实现我的目标的任何提示?提前致谢!

【问题讨论】:

  • 你如何指定你需要哪些孩子?
  • “语义”数组具有独特的内容。所以我必须检查数组内容

标签: node.js mongodb projection


【解决方案1】:

试试这个

db.getCollection('model').aggregate([{
        $unwind: "$model.dataList"
    },
    {
        $unwind: "$model.dataList.subDataList"
    },
    {
        $unwind: "$model.dataList.subDataList.subDataList"
    },
    {
        $group: {
            _id: '$_id',
            subDataList: {
                $first: '$model.dataList.subDataList.subDataList'
            }
        }
    }
])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 2015-05-12
    • 2021-04-16
    • 2018-07-22
    相关资源
    最近更新 更多