【问题标题】:Spring Data Mongo - How to get the nested distinct array for nested value?Spring Data Mongo - 如何获取嵌套值的嵌套不同数组?
【发布时间】:2020-09-27 07:31:12
【问题描述】:

我正在参考: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"
                    }
                ]
            }
        ]
    },
}

【问题讨论】:

  • 不清楚问题标题中的 distinct array 是什么意思。可能会发布示例输出。

标签: mongodb aggregation-framework spring-data-mongodb


【解决方案1】:

您可以通过此聚合获得不同的技术(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" }
  }
] )

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2023-01-04
  • 2020-05-26
  • 1970-01-01
  • 2018-04-18
  • 2017-04-17
  • 2023-01-28
相关资源
最近更新 更多