基于MongoDB中不同字段的聚合结果
我试图在单个查询中获取来自同一集合的所有所有者的位置明智数据和预算明智数据的计数。将来我们可能还需要显示基于项目的报告。 以下是样本集合。
[
{
"owner": 1,
"location": "Goa",
"budget": "1 Cr and Above",
"project": "ABC"
},
{
"owner": 1,
"location": "Goa",
"budget": "10 - 30 Lacs",
"project": "ABC"
},
{
"owner": 1,
"location": "Mumbai",
"budget": "30 - 50 Lacs",
"project": "XYZ"
},
{
"owner": 2,
"location": "Delhi",
"budget": "30 - 50 Lacs",
"project": "ABC"
},
{
"owner": 2,
"location": "Delhi",
"budget": "1 Cr and Above",
"project": "DEF"
},
{
"owner": 2,
"location": "Delhi",
"budget": "30 - 50 Lacs",
"project": "ABC"
},
{
"owner": 3,
"location": "Goa",
"budget": "70 - 90 Lacs",
"project": "DEF"
},
{
"owner": 3,
"location": "Mumbai",
"budget": "70 - 90 Lacs",
"project": "XYZ"
},
{
"owner": 4,
"location": "Bangalore",
"budget": "2 Cr and Above",
"project": "DEF"
},
{
"owner": 4,
"location": "Goa",
"budget": "2 Cr and Above",
"project": "ABC"
}
]
以下是我正在执行的查询,以获取所有者的位置明智记录:
db.collection.aggregate([
{
"$group": {
"_id": {
"owner": "$owner",
"location": "$location",
"budget": "$budget"
},
"num": {
"$sum": 1
}
}
},
{
"$group": {
"_id": "$_id.owner",
"location": {
"$push": {
"location": "$_id.location",
"count": "$num"
}
}
}
},
{
"$project": {
"owner": "$_id",
"_id": false,
"location": 1,
"totalLocation": {
"$sum": "$location.count"
},
"totalBudget": {
"$sum": "$budget.count"
}
}
}
])
输出如下所示。
[
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Bangalore"
}
],
"owner": 4,
"totalLocation": 2
},
{
"location": [
{
"count": 3,
"location": "Delhi"
}
],
"owner": 2,
"totalLocation": 3
},
{
"location": [
{
"count": 1,
"location": "Mumbai"
},
{
"count": 2,
"location": "Goa"
}
],
"owner": 1,
"totalLocation": 3
},
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Mumbai"
}
],
"owner": 3,
"totalLocation": 2
}
]
我该如何修改相同的查询才能在同一结果集中获取预算明智的数据?像下面这样的东西。
[
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Bangalore"
}
],
"budget": [
{
"count": 2,
"budget": "2 Cr and Above"
}
],
"owner": 4,
"totalLocation": 2,
"totalBudget": 2,
},
{
"location": [
{
"count": 3,
"location": "Delhi"
}
],
"budget": [
{
"count": 2,
"budget": "30 - 50 Lacs"
},
{
"count": 1,
"budget": "1 Cr and Above"
}
],
"owner": 2,
"totalLocation": 3,
"totalBudget": 2
},
{
"location": [
{
"count": 1,
"location": "Mumbai"
},
{
"count": 2,
"location": "Goa"
}
],
"budget": [
{
"count": 1,
"budget": "1 Cr and Above"
},
{
"count": 1,
"budget": "10 - 30 Lacs"
},
{
"count": 1,
"budget": "30 - 50 Lacs"
}
],
"owner": 1,
"totalLocation": 3,
"totalBudget": 3
},
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Mumbai"
}
],
"budget": [
{
"count": 1,
"budget": "70 - 90 Lacs"
}
],
"owner": 3,
"totalLocation": 2,
"totalBudget": 1
}
]
I am trying to get the count of location wise data and budget wise data in a single query for all owners from the same collection. In future we may need to show reports based on project wise as well.
Below is the sample collection.
[
{
"owner": 1,
"location": "Goa",
"budget": "1 Cr and Above",
"project": "ABC"
},
{
"owner": 1,
"location": "Goa",
"budget": "10 - 30 Lacs",
"project": "ABC"
},
{
"owner": 1,
"location": "Mumbai",
"budget": "30 - 50 Lacs",
"project": "XYZ"
},
{
"owner": 2,
"location": "Delhi",
"budget": "30 - 50 Lacs",
"project": "ABC"
},
{
"owner": 2,
"location": "Delhi",
"budget": "1 Cr and Above",
"project": "DEF"
},
{
"owner": 2,
"location": "Delhi",
"budget": "30 - 50 Lacs",
"project": "ABC"
},
{
"owner": 3,
"location": "Goa",
"budget": "70 - 90 Lacs",
"project": "DEF"
},
{
"owner": 3,
"location": "Mumbai",
"budget": "70 - 90 Lacs",
"project": "XYZ"
},
{
"owner": 4,
"location": "Bangalore",
"budget": "2 Cr and Above",
"project": "DEF"
},
{
"owner": 4,
"location": "Goa",
"budget": "2 Cr and Above",
"project": "ABC"
}
]
Below is the query I am executing to get location wise record for a owner:
db.collection.aggregate([
{
"$group": {
"_id": {
"owner": "$owner",
"location": "$location",
"budget": "$budget"
},
"num": {
"$sum": 1
}
}
},
{
"$group": {
"_id": "$_id.owner",
"location": {
"$push": {
"location": "$_id.location",
"count": "$num"
}
}
}
},
{
"$project": {
"owner": "$_id",
"_id": false,
"location": 1,
"totalLocation": {
"$sum": "$location.count"
},
"totalBudget": {
"$sum": "$budget.count"
}
}
}
])
The output is like below.
[
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Bangalore"
}
],
"owner": 4,
"totalLocation": 2
},
{
"location": [
{
"count": 3,
"location": "Delhi"
}
],
"owner": 2,
"totalLocation": 3
},
{
"location": [
{
"count": 1,
"location": "Mumbai"
},
{
"count": 2,
"location": "Goa"
}
],
"owner": 1,
"totalLocation": 3
},
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Mumbai"
}
],
"owner": 3,
"totalLocation": 2
}
]
How shall I modify the same query to get the budget wise data also in the same result set ? Something like below.
[
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Bangalore"
}
],
"budget": [
{
"count": 2,
"budget": "2 Cr and Above"
}
],
"owner": 4,
"totalLocation": 2,
"totalBudget": 2,
},
{
"location": [
{
"count": 3,
"location": "Delhi"
}
],
"budget": [
{
"count": 2,
"budget": "30 - 50 Lacs"
},
{
"count": 1,
"budget": "1 Cr and Above"
}
],
"owner": 2,
"totalLocation": 3,
"totalBudget": 2
},
{
"location": [
{
"count": 1,
"location": "Mumbai"
},
{
"count": 2,
"location": "Goa"
}
],
"budget": [
{
"count": 1,
"budget": "1 Cr and Above"
},
{
"count": 1,
"budget": "10 - 30 Lacs"
},
{
"count": 1,
"budget": "30 - 50 Lacs"
}
],
"owner": 1,
"totalLocation": 3,
"totalBudget": 3
},
{
"location": [
{
"count": 1,
"location": "Goa"
},
{
"count": 1,
"location": "Mumbai"
}
],
"budget": [
{
"count": 1,
"budget": "70 - 90 Lacs"
}
],
"owner": 3,
"totalLocation": 2,
"totalBudget": 1
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
在这种情况下,我将编辑我的原始响应并建议:
您可以看到在这里输入数据的更改,以证明功能。
Edit:
In this case, I would edit my original response and suggest:
Which you can see here with small changes in the input data, in order to demonstrate the functionality.