Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results

江枫思渺然 提交于 2020-05-08 05:56:05

问题


I'm developing Spring Boot and Spring Data Mongo example. In this example, I want to get the distinct departments only, but I dont want to fecth subdepartments. What Query do I need to change?

db.employees.distinct("departments");

Data:

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

    "departments" : {
        "deptCd" : "Tax",
        "deptName" : "Tax Handling Dept",
        "status" : "A",
        "subdepts" : [ 
            {
                "subdeptCd" : "1D",
                "subdeptName" : "Tax Clearning",
                "desc" : "",
                "status" : "A"
            }
        ]
    },
}

回答1:


The aggregation gets the distinct departments.deptCd values (plus other details):

db.collection.aggregate( [
{
    $group: { _id: "$departments.deptCd", 
             deptName: { $first: "$departments.deptName" },
             status: { $first: "$departments.status" }
    }
},
{
    $project: { deptCd: "$_id", _id: 0, deptName: 1, status: 1 }
}
] )

The output:

{ "deptName" : "Tax Handling Dept", "status" : "A", "deptCd" : "Tax" }


来源:https://stackoverflow.com/questions/61608104/spring-data-mongo-perform-distinct-but-doesnt-wants-to-pull-embedded-documen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!