Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 仅返回嵌套数组中具有所有属性的特定元素_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb 仅返回嵌套数组中具有所有属性的特定元素

Mongodb 仅返回嵌套数组中具有所有属性的特定元素,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有以下文档结构(它是一个小型LMS): 简而言之,我有一份包含所有课程的文档,一门课程由几个级别组成,每个级别都有许多模块:课程>级别>模块 (这里有一个现成的游乐场:) 知道代码或_id,我必须提取单一模式,但我还必须保持文档的结构不变,并保持父文档的属性。 我的预期输出是(当我查找代码为MC1_02的模块时): 我正在尝试几次尝试,并且已经尝试使用这些答案中强调的解决方案: 我确切地知道如何使用$unwind和$group进行操作,但对我来说这不是最好的解决方案,我更喜欢使用$map

我有以下文档结构(它是一个小型LMS):

简而言之,我有一份包含所有课程的文档,一门课程由几个级别组成,每个级别都有许多模块:课程>级别>模块

(这里有一个现成的游乐场:)

知道代码或_id,我必须提取单一模式,但我还必须保持文档的结构不变,并保持父文档的属性。 我的预期输出是(当我查找代码为MC1_02的模块时):

我正在尝试几次尝试,并且已经尝试使用这些答案中强调的解决方案:




我确切地知道如何使用$unwind和$group进行操作,但对我来说这不是最好的解决方案,我更喜欢使用$map和$filter。 同时,我不能使用$project或任何其他聚合运算符强制我逐个枚举所有属性,因为在任何级别都有大量不同的属性,我无法列出它们,我只需要保留所有属性

万分感谢那些帮助我的人:)

  • $match
    匹配
    code
    条件
  • $map
    迭代
    级别的循环
    数组
  • $filter
    迭代
    模块的循环
    数组和具有
    代码的
    模块
    ,“MC1_02”
  • $mergeObjects
    将当前对象与更新的
    模块
    字段合并
  • $filter
    如果
    模块
    不为空,则显示上述结果[]

[
  {
    "name": "course1",
    "code": "C1",
    "levels": [
      {
        "levelname": "level01",
        "order": 1,
        "modules": [
          {
            "modulename": "title module 01",
            "code": "MC1_01",
            "order": 1,
            "title": "module title 01"
          },
          {
            "modulename": "title module 02",
            "code": "MC1_02",
            "order": 2,
            "title": "module title 02"
          }
        ]
      },
      {
        "levelname": "level02",
        "modules": [
          {
            "modulename": "title module 11",
            "code": "MC1_11",
            "order": 1,
            "title": "module title 01"
          },
          {
            "modulename": "title module 12",
            "code": "MC1_12",
            "order": 2,
            "title": "module title 02"
          }
        ]
      }
    ]
  },
  {
    "name": "course2",
    "code": "C2",
    "levels": [
      {
        "levelname": "level01",
        "order": 1,
        "modules": [
          {
            "modulename": "title module 01",
            "code": "MC2_01",
            "order": 1,
            "title": "module title 01"
          },
          {
            "modulename": "title module 02",
            "code": "MC2_02",
            "order": 2,
            "title": "module title 02"
          }
        ]
      },
      {
        "levelname": "level02",
        "modules": [
          {
            "modulename": "title module 10",
            "code": "MC2_10",
            "order": 1,
            "title": "module title 01"
          },
          {
            "modulename": "title module 11",
            "code": "MC2_11",
            "order": 2,
            "title": "module title 02"
          }
        ]
      }
    ]
  }
]
{
    "name": "course1",
    "code": "C1",
    "levels": [
        {
            "levelname": "level01",
            "order": 1,
            "modules": [
                {
                    "modulename": "title module 02",
                    "code": "MC1_02",
                    "order": 2,
                    "title": "module title 02"
                }
            ]
        }
    ]
}
db.collection.aggregate([
  { $match: { "levels.modules.code": "MC1_02" } },
  {
    $addFields: {
      levels: {
        $filter: {
          input: {
            $map: {
              input: "$levels",
              in: {
                $mergeObjects: [
                  "$$this",
                  {
                    modules: {
                      $filter: {
                        input: "$$this.modules",
                        cond: { $eq: ["$$this.code", "MC1_02"] }
                      }
                    }
                  }
                ]
              }
            }
          },
          cond: { $ne: ["$$this.modules", []] }
        }
      }
    }
  }
])