如何查询数据,并按月将它们分组以供给线路图?
我面临一些问题,我不知道我应该在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Playground
Playground