Spring Data Mongo-如何获取嵌套的嵌套数组以获取嵌套值?

问题描述 投票:1回答:1

我从Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results获得参考,并提出其他问题。

我想找到“ 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"
                    }
                ]
            }
        ]
    },
}
mongodb aggregation-framework spring-data-mongodb
1个回答
0
投票

通过这种聚合,您可以获得独特的技术(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" }
  }
] )
© www.soinside.com 2019 - 2024. All rights reserved.