Spring Data Mongo-如何获取嵌套的嵌套数组以获取嵌套值? [英] Spring Data Mongo - How to get the nested distinct array for nested value?

查看:161
本文介绍了Spring Data Mongo-如何获取嵌套的嵌套数组以获取嵌套值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从以下地址引用:

I'm taking a reference from : Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results and asking another questions.

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

I want to find technology list where "subdeptCd" : "1D". How can we do that ?

{
    "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数组元素):

You can get distinct technologies (technology array elements) with this aggregation:

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" }
  }
] )

这篇关于Spring Data Mongo-如何获取嵌套的嵌套数组以获取嵌套值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆