Spring Data Mongo-执行不同的操作,但不想在结果中提取嵌入式文档

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

我正在开发Spring Boot and Spring Data Mongo示例。在此示例中,我只想获得不同的部门,但是我不想影响子部门。我需要更改哪些查询?

db.employees.distinct("departments");

数据:

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

    "departments" : {
        "deptCd" : "Tax",
        "deptName" : "Tax Handling Dept",
        "status" : "A",
        "subdepts" : [ 
            {
                "subdeptCd" : "1D",
                "subdeptName" : "Tax Clearning",
                "desc" : "",
                "status" : "A"
            }
        ]
    },
}
mongodb spring-data-mongodb
1个回答
0
投票

聚合获得不同的departments.deptCd值(加上其他详细信息:

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

输出:

{ "deptName" : "Tax Handling Dept", "status" : "A", "deptCd" : "Tax" }
© www.soinside.com 2019 - 2024. All rights reserved.