MongoDB-$ filter中的聚合$ filter /子文档中子文档的过滤数组

尘土飞扬48

我在集合中具有以下文档结构:

工厂有很多部门,有很多区域。

factoryName: "",
departments: [
   { 
      departmentName: ""
      areas: [
         {
            areaName: ""
         } 
      ]
   }
]

通过“ areaName”查询文档时,我只想获取区域+各自的上级部门+各自的上级工厂。

例如,请简要查看以下2个文档。

db.factories.insertMany([{
  factoryName: "San Francisco",
  departments: [
    {
      departmentName: "Administration",
      areas: [
        {
          areaName: "Phone Guys"
        },
        {
          areaName: "Email Guys"
        }
      ]
    },
    {
      departmentName: "Development",
      areas: [
        {
          areaName: "Dev Ops"
        },
        {
          areaName: "Programming"
        },
        {
          areaName: "Architecture"
        }
      ]
    }
  ]
},{
  factoryName: "Chicago",
  departments: [
    {
      departmentName: "Administration",
      areas: [
        {
          areaName: "Phone Guys"
        },
        {
          areaName: "Email Guys"
        }
      ]
    },
    {
      departmentName: "Logistics",
      areas: [
        {
          areaName: "Delivery"
        },
        {
          areaName: "Human Resources"
        }
      ]
    }
  ]
}])

我希望查询areaName = "Architecture"并收到以下信息:

factoryName: "San Francisco",
departments: [
   { 
      departmentName: "Development"
      areas: [
         {
            areaName: "Architecture"
         } 
      ]
   }
]

由于通过.find()与投影进行常规查询组合失败(到目前为止),因此我尝试进行聚合。为了获得理想的结果,我尝试了很多事情,但是在过滤区域时失败了。筛选部门的工作。

使用MongoDB Compass视觉聚合功能,对我来说最合乎逻辑的是:

db.factories.aggregate([
  {
    '$match': {
      'departments.areas.areaName': 'Architecture'
    }
  }, {
    '$addFields': {
      'departments': {
        '$filter': {
          'input': '$departments', 
          'as': 'department', 
          'cond': {
            '$and': [
              {
                '$eq': [
                  '$$department.departmentName', 'Development'
                ]
              }, {
                '$filter': {
                  'input': '$$department.areas', 
                  'as': 'area', 
                  'cond': {
                    '$eq': [
                      '$$area.areaName', 'Architecture'
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])

似乎$filter里面的a$filter不起作用,或者我在这里丢失了一些东西,因为返回了“开发”部门的所有3个区域,而不是仅返回“建筑”部门,而“开发”部门却被正确过滤。

我该如何实现?我想念什么?任何帮助深表感谢。

非常感谢!

米克尔

您需要$ filter$ map,因为您有嵌套数组:

db.collection.aggregate([
    {   
        "$match": {
            "departments.areas.areaName": "Architecture"
        }
    },
    {
        $addFields: {
            departments: {
                $map: {
                    input: {
                        $filter: {
                            input: "$departments",
                            cond: {
                                $in: [ "Architecture", "$$this.areas.areaName" ]
                            }
                        }
                    },
                    in: {
                        departmentName: "$$this.departmentName",
                        areas: { $filter: { input: "$$this.areas", as: "d", cond: { $eq: [ "$$d.areaName", "Architecture" ] } } }
                    }
                }
            }
        }
    }
])

蒙哥运动场

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章