我正在开发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"
}
]
},
}
聚合获得不同的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" }