Spring Data Mongo-执行不同的操作,但不想在结果中提取嵌入式文档 [英] Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results

查看:74
本文介绍了Spring Data Mongo-执行不同的操作,但不想在结果中提取嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");

数据:

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

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

推荐答案

聚合将获得不同的departments.deptCd值(以及其他详细信息):

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

输出:

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

这篇关于Spring Data Mongo-执行不同的操作,但不想在结果中提取嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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