Spring 数据Mongo - 如何获取嵌套值的嵌套不同数组?

Spring Data Mongo - How to get the nested distinct array for nested value?

我参考了 : 并提出了另一个问题。

我想找到 "subdeptCd" 中的技术列表:“1D”。我们该怎么做?

{
    "firstName" : "Laxmi",
    "lastName" : "Dekate",
    .....
    .......
    .....

    "departments" : {
        "deptCd" : "Tax",
        "deptName" : "Tax Handling Dept",
        "status" : "A",
        "subdepts" : [ 
            {
                "subdeptCd" : "1D",
                "subdeptName" : "Tax Clearning",
                "desc" : "",
                "status" : "A"
                "technology" : [ 
                    {
                        "technologyCd" : "4C",
                        "technologyName" : "Cloud Computing",
                        "desc" : "This is best certficate",
                        "status" : "A"
                    }
                ]
            }
        ]
    },
},
{
    "firstName" : "Neha",
    "lastName" : "Parate",
    .....
    .......
    .....

    "departments" : {
        "deptCd" : "Tax Rebate",
        "deptName" : "Tax Rebate Handling Dept",
        "status" : "A",
        "subdepts" : [ 
            {
                "subdeptCd" : "1D",
                "subdeptName" : "Tax Clearning",
                "desc" : "",
                "status" : "A"
                "technology" : [ 
                    {
                        "technologyCd" : "9C",
                        "technologyName" : "Spring Cloud",
                        "desc" : "This is best certficate post Google",
                        "status" : "A"
                    }
                ]
            }
        ]
    },
}

您可以通过此聚合获得不同的技术(technology 个数组元素):

db.depts.aggregate( [
  {
       $unwind: "$departments.subdepts"
  },
  {
       $unwind: "$departments.subdepts.technology"
  },
  {
       $match: { "departments.subdepts.subdeptCd": "1D" }
  },
  {
       $group: { _id: "$departments.subdepts.technology.technologyCd", tech: { $first: "$departments.subdepts.technology" } }
  },
  {
      $replaceRoot: { newRoot: "$tech" }
  }
] )