我正在使用mongodb并尝试构建一个groupby查询。一个文档看起来像这样
{
"_id" : "ca5ff110697611e89c2d9a0012d6e110",
"CreatedAt" : ISODate("2018-06-06T16:15:55.000Z,
"CreatedBy" : {
"Email" : "neo@orangescape.com",
"Name" : "Neo",
"_id" : "User001"
},
},
"Salary" : 200000
"Branch" : "C2"
}
我的groupby查询看起来像这样
[{'$project': {'Branch': 1, 'CreatedAt.year':{$year:"$CreatedAt"}, 'CreatedBy': 1, 'Salary': 1}},
{'$group': {'_id': {'Branch': '$Branch',
'Salary': '$Salary',
'CreatedAt.year': "$CreatedAt.year" ,
'CreatedBy': '$CreatedBy._id'},
'Salary': {'$sum': '$Salary'},
'CreatedBy': "$CreatedBy"
}},
]
如您所见,CreatedBy
是一个嵌套字段,在其_id
字段上分组仅产生_id
字段,但我想保留name
和{{1 }}。按整个email
进行分组,因为用户可能会在某个时候更改其名称,而旧文档将具有旧名称。
答案 0 :(得分:1)
$group
阶段的每个字段都需要一个累加器运算符。要保留CreatedBy
字段,可以使用$first
例如:
新查询:
db.collection.aggregate([
{
"$project": {
"Branch": 1,
"CreatedAt.year": {
$year: "$CreatedAt"
},
"CreatedBy": 1,
"Salary": 1
}
},
{
"$group": {
"_id": {
"Branch": "$Branch",
"Salary": "$Salary",
"CreatedAtYear": "$CreatedAt.year",
"CreatedBy": "$CreatedBy._id"
},
"Salary": {
"$sum": "$Salary"
},
"CreatedBy": {
"$first": "$CreatedBy"
}
}
}
])
结果:
[
{
"CreatedBy": {
"Email": "neo@orangescape.com",
"Name": "Neo",
"_id": "User001"
},
"Salary": 200000,
"_id": {
"Branch": "C2",
"CreatedAtYear": 2018,
"CreatedBy": "User001",
"Salary": 200000
}
}
]
您可以在此处在线尝试:mongoplayground.net/p/cVNHRD5Rv21