如何查询数据,并按月将它们分组以供给线路图?

发布于 2025-01-30 09:10:37 字数 1061 浏览 2 评论 0原文

我面临一些问题,我不知道我应该在MongoDB文档中搜索什么关键字。

因此,问题是,我现在有一个用户现在创作的交易记录列表,例如以下。

[
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 80,
        "createdAt": "2022-04-18T06:59:07.836Z"
    },
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 70,
        "createdAt": "2022-04-19T06:59:07.836Z"
    },
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 55,
        "createdAt": "2022-05-18T06:59:07.836Z"
    },
        ...
]

在我现在正在使用的仪表板应用程序中,有一个线图。要馈送线路图,我需要以某种方式对我拥有的交易的原始数据进行查询/汇总,并将它们分组为一系列对象,其中包含该月期间的几个月和总支出。

[
    {
      "period": "2022-04",
      "totalSpending": 900
    },
    {
      "period": "2022-05",
      "totalSpending": 2000
    },
    {
      "period": "2022-06",
      "totalSpending": 367
    },
      ...
]

从技术上讲,我拥有的数据在技术上是否有可能将它们组成数月,如上所述?

如果提供任何信息,将不胜感激。谢谢

I'm facing some issue and I have no idea what keyword should I search for in the MongoDB docs.

So the problem is, I have a list of transactions record made by users right now, eg as below.

[
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 80,
        "createdAt": "2022-04-18T06:59:07.836Z"
    },
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 70,
        "createdAt": "2022-04-19T06:59:07.836Z"
    },
    {
        "_id": Some_Mongo_ID,
        "invoiceId": "ABCDEFG",
        "username": "randomUser"
        "amount": 55,
        "createdAt": "2022-05-18T06:59:07.836Z"
    },
        ...
]

In a Dashboard app that I'm working with right now, there's a line chart. To feed the line chart I will need to somehow make query/aggregation on the raw data of Transactions that I have, and group them into an array of objects, containing the months and total spend during that month.

[
    {
      "period": "2022-04",
      "totalSpending": 900
    },
    {
      "period": "2022-05",
      "totalSpending": 2000
    },
    {
      "period": "2022-06",
      "totalSpending": 367
    },
      ...
]

Is it technically possible to query/aggregate data that I have and make them group by months into an array as showed above?

Would be appreciated if any info is provided. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

坏尐絯℡ 2025-02-06 09:10:37

Playground

db.collection.aggregate([
  {
    "$group": {
      "_id": {
        month: { //Group by Month
          "$month": "$createdAt"
        },
        year: { //Group by Year
          "$year": "$createdAt"
        }
      },
      "totalSpending": { //Accumulate sum for the group
        $sum: "$amount"
      }
    }
  },
  {
    "$project": { //You can ignore this if the group format is ok
      "totalSpending": 1,
      "period": { //To form the expected string
        "$concat": [
          {
            $toString: "$_id.year"
          },
          "-",
          {
            $toString: "$_id.month"
          }
        ]
      },
      "_id": 0
    }
  }
])

Playground

db.collection.aggregate([
  {
    "$group": {
      "_id": {
        month: { //Group by Month
          "$month": "$createdAt"
        },
        year: { //Group by Year
          "$year": "$createdAt"
        }
      },
      "totalSpending": { //Accumulate sum for the group
        $sum: "$amount"
      }
    }
  },
  {
    "$project": { //You can ignore this if the group format is ok
      "totalSpending": 1,
      "period": { //To form the expected string
        "$concat": [
          {
            $toString: "$_id.year"
          },
          "-",
          {
            $toString: "$_id.month"
          }
        ]
      },
      "_id": 0
    }
  }
])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文