以嵌套格式显示 mongodb 多个分组查询结果
Display mongodb multiple group by query results in a nested format
给定以下数据集:
[
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "20",
"hours": "18"
},
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "20",
"hours": "12"
},
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "21",
"hours": "10"
},
{
"account_id" : "1111"
"task_id" : "bbbb",
"workweek" : "21",
"hours": "5"
},
{
"account_id" : "2222"
"task_id" : "cccc",
"workweek" : "21",
"hours": "15"
}
]
我想对文档进行分组并得到以下格式的结果:
[
{
"account_id": "1111",
"tasks": [
{
"task_id": "aaaa",
"workweeks": [
{
"workweek": "20",
"total_hours": "30"
},
{
"workweek": "21",
"total_hours": "10"
}
]
},
{
"task_id": "bbbb",
"workweeks": [
{
"workweek": "21",
"total_hours": "5"
}
]
}
]
},
{
"account_id": "2222",
"tasks": [
{
"task_id": "cccc",
"workweeks": [
{
"workweek": "21",
"total_hours": "15"
}
]
},
]
}
]
我的破解聚合代码如下:
db.tasks.aggregate([
{"$group": {
"_id": {
"account_id": "$account_id",
"task_id": "$task_id",
"workweek": "$workweek",
},
"total_hours": {$sum: "$hours"}
}},
{"$group": {
"_id": "$_id.account_id",
"tasks": {
"$push": {
"task_id": "$_id.task_id",
"workweeks": {
"$push": {
"workweek": "$_id.workweek",
"total_hours": "$total_hours"
}
}
}
}
}}
]);
我收到此错误:
"errmsg" : "exception: invalid operator '$push'",
我认为这是因为我只能为每个 $group 块 $push 一次。
关于如何实现预期结果有什么想法吗?
请再尝试一个(中间件)"group-by" 步骤:
db.tasks.aggregate([
{"$group": {
"_id": {
"account_id": "$account_id",
"task_id": "$task_id",
"workweek": "$workweek",
},
"total_hours": {$sum: "$hours"}
}},
{"$group": {
"_id": {
"$_id.account_id",
"$_id.task_id"
},
"workweeks": {
"$push": {
"workweek": "$_id.workweek",
"total_hours": "$total_hours"
}
}
}},
{"$group": {
"_id": "$_id.account_id",
"tasks": {
"$push": {
"task_id": "$_id.task_id",
"workweeks": "$workweeks"
}
}
}}
]);
给定以下数据集:
[
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "20",
"hours": "18"
},
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "20",
"hours": "12"
},
{
"account_id" : "1111"
"task_id" : "aaaa",
"workweek" : "21",
"hours": "10"
},
{
"account_id" : "1111"
"task_id" : "bbbb",
"workweek" : "21",
"hours": "5"
},
{
"account_id" : "2222"
"task_id" : "cccc",
"workweek" : "21",
"hours": "15"
}
]
我想对文档进行分组并得到以下格式的结果:
[
{
"account_id": "1111",
"tasks": [
{
"task_id": "aaaa",
"workweeks": [
{
"workweek": "20",
"total_hours": "30"
},
{
"workweek": "21",
"total_hours": "10"
}
]
},
{
"task_id": "bbbb",
"workweeks": [
{
"workweek": "21",
"total_hours": "5"
}
]
}
]
},
{
"account_id": "2222",
"tasks": [
{
"task_id": "cccc",
"workweeks": [
{
"workweek": "21",
"total_hours": "15"
}
]
},
]
}
]
我的破解聚合代码如下:
db.tasks.aggregate([
{"$group": {
"_id": {
"account_id": "$account_id",
"task_id": "$task_id",
"workweek": "$workweek",
},
"total_hours": {$sum: "$hours"}
}},
{"$group": {
"_id": "$_id.account_id",
"tasks": {
"$push": {
"task_id": "$_id.task_id",
"workweeks": {
"$push": {
"workweek": "$_id.workweek",
"total_hours": "$total_hours"
}
}
}
}
}}
]);
我收到此错误: "errmsg" : "exception: invalid operator '$push'", 我认为这是因为我只能为每个 $group 块 $push 一次。
关于如何实现预期结果有什么想法吗?
请再尝试一个(中间件)"group-by" 步骤:
db.tasks.aggregate([
{"$group": {
"_id": {
"account_id": "$account_id",
"task_id": "$task_id",
"workweek": "$workweek",
},
"total_hours": {$sum: "$hours"}
}},
{"$group": {
"_id": {
"$_id.account_id",
"$_id.task_id"
},
"workweeks": {
"$push": {
"workweek": "$_id.workweek",
"total_hours": "$total_hours"
}
}
}},
{"$group": {
"_id": "$_id.account_id",
"tasks": {
"$push": {
"task_id": "$_id.task_id",
"workweeks": "$workweeks"
}
}
}}
]);