基于MongoDB中不同字段的聚合结果

发布于 2025-01-18 01:37:06 字数 4076 浏览 0 评论 0原文

我试图在单个查询中获取来自同一集合的所有所有者的位置明智数据和预算明智数据的计数。将来我们可能还需要显示基于项目的报告。 以下是样本集合。

[
  {
    "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2025-01-25 01:37:06

编辑:

在这种情况下,我将编辑我的原始响应并建议:

db.collection.aggregate([
  {
    $facet: {
      budgets: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "budget": "$budget"
            },
            budgetCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            budgets: {
              "$addToSet": {
                budget: "$_id.budget",
                count: "$budgetCount"
              }
            },
            totalBudgets: {$sum: "$budgetCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            budgets: 1,
            totalBudgets: 1,
            locations: null,
            projects: null
          }
        }
      ],
      locations: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "location": "$location"
            },
            locationsCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            locations: {
              "$addToSet": {
                location: "$_id.location",
                count: "$budgetCount"
              }
            },
            totalLocations: {$sum: "$locationsCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            locations: 1,
            totalLocations: 1,
            budgets: null,
            projects: null
          }
        }
      ],
      projects: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "project": "$project"
            },
            projectsCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            projects: {
              "$addToSet": {
                project: "$_id.project",
                count: "$projectsCount"
              }
            },
            totalProjects: {$sum: "$projectsCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            projects: 1,
            totalProjects: 1,
            budgets: null,
            locations: null
          }
        }
      ],
    }
  },
  {
    $project: {
      data: {
        "$concatArrays": [
          "$budgets",
          "$locations",
          "$projects"
        ]
      }
    }
  },
  {
    $unwind: "$data"
  },
  {
    $group: {
      _id: "$data.owner",
      locations: {
        $push: {
          $ifNull: ["$data.locations", "$REMOVE"]
        }
      },
      budgets: {
        $push: {
          $ifNull: ["$data.budgets", "$REMOVE"]
        }
      },
      projects: {
        $push: {
          $ifNull: ["$data.projects", "$REMOVE"]
        }
      },
      totalBudgets: {$sum: "$data.totalBudgets"},
      totalLocations: {$sum: "$data.totalLocations"},
      totalProjects: {$sum: "$data.totalProjects"},   
    }
  },
  {
    "$project": {
      "_id": 0,
      "owner": "$_id",
      "locations": {"$arrayElemAt": ["$locations", 0]},
      "projects": {"$arrayElemAt": ["$projects", 0]},
      "budget": {"$arrayElemAt": ["$budgets", 0]},
      totalBudgets: 1,
      totalLocations: 1,
      totalProjects: 1
    }
  }
])

您可以看到在这里输入数据的更改,以证明功能。

Edit:

In this case, I would edit my original response and suggest:

db.collection.aggregate([
  {
    $facet: {
      budgets: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "budget": "$budget"
            },
            budgetCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            budgets: {
              "$addToSet": {
                budget: "$_id.budget",
                count: "$budgetCount"
              }
            },
            totalBudgets: {$sum: "$budgetCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            budgets: 1,
            totalBudgets: 1,
            locations: null,
            projects: null
          }
        }
      ],
      locations: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "location": "$location"
            },
            locationsCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            locations: {
              "$addToSet": {
                location: "$_id.location",
                count: "$budgetCount"
              }
            },
            totalLocations: {$sum: "$locationsCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            locations: 1,
            totalLocations: 1,
            budgets: null,
            projects: null
          }
        }
      ],
      projects: [
        {
          "$group": {
            "_id": {
              "owner": "$owner",
              "project": "$project"
            },
            projectsCount: {$sum: 1}
          }
        },
        {
          "$group": {
            "_id": {"owner": "$_id.owner"},
            projects: {
              "$addToSet": {
                project: "$_id.project",
                count: "$projectsCount"
              }
            },
            totalProjects: {$sum: "$projectsCount"},
          }
        },
        {
          $project: {
            owner: "$_id.owner",
            _id: 0,
            projects: 1,
            totalProjects: 1,
            budgets: null,
            locations: null
          }
        }
      ],
    }
  },
  {
    $project: {
      data: {
        "$concatArrays": [
          "$budgets",
          "$locations",
          "$projects"
        ]
      }
    }
  },
  {
    $unwind: "$data"
  },
  {
    $group: {
      _id: "$data.owner",
      locations: {
        $push: {
          $ifNull: ["$data.locations", "$REMOVE"]
        }
      },
      budgets: {
        $push: {
          $ifNull: ["$data.budgets", "$REMOVE"]
        }
      },
      projects: {
        $push: {
          $ifNull: ["$data.projects", "$REMOVE"]
        }
      },
      totalBudgets: {$sum: "$data.totalBudgets"},
      totalLocations: {$sum: "$data.totalLocations"},
      totalProjects: {$sum: "$data.totalProjects"},   
    }
  },
  {
    "$project": {
      "_id": 0,
      "owner": "$_id",
      "locations": {"$arrayElemAt": ["$locations", 0]},
      "projects": {"$arrayElemAt": ["$projects", 0]},
      "budget": {"$arrayElemAt": ["$budgets", 0]},
      totalBudgets: 1,
      totalLocations: 1,
      totalProjects: 1
    }
  }
])

Which you can see here with small changes in the input data, in order to demonstrate the functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文