Mongodb-获得每小时销售额
好人!我需要你的帮助 我正在尝试使用apexcharts和从Mongodb导入的数据创建一个折线图。 我正在尝试绘制每小时销售额的图表,因此我需要一天中每小时的销售额 示例Mongodb文档。Mongodb-获得每小时销售额,mongodb,apexcharts,Mongodb,Apexcharts,好人!我需要你的帮助 我正在尝试使用apexcharts和从Mongodb导入的数据创建一个折线图。 我正在尝试绘制每小时销售额的图表,因此我需要一天中每小时的销售额 示例Mongodb文档。 { "_id" : ObjectId("5dbee4eed6f04aaf191abc59"), "seller_id" : "5aa1c2c35ef7a4e97b5e995a", "temp" : "4.3", "sale_type" : "coins", "cre
{
"_id" : ObjectId("5dbee4eed6f04aaf191abc59"),
"seller_id" : "5aa1c2c35ef7a4e97b5e995a",
"temp" : "4.3",
"sale_type" : "coins",
"createdAt" : ISODate("2020-05-10T00:10:00.000Z"),
"updatedAt" : ISODate("2019-11-10T14:32:14.650Z")
}
到目前为止,我有一个这样的问题:
db.getCollection('sales').aggregate([
{ "$facet": {
"00:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T00:00:00.000Z"),$lt: ISODate("2020-05-10T00:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"01:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T01:00:00.000Z"),$lt: ISODate("2020-05-10T01:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"02:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T02:00:00.000Z"),$lt: ISODate("2020-05-10T02:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"03:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T03:00:00.000Z"),$lt: ISODate("2020-05-10T03:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
}},
{ "$project": {
"ventas0": { "$arrayElemAt": ["$01:00.sales", 0] },
"ventas1": { "$arrayElemAt": ["$02:00.sales", 0] },
"ventas3": { "$arrayElemAt": ["$03:00.sales", 0] },
}}
])
[countsale(00:00),countsale(01:00),countsale(02:00),countsale(03:00), etc to 24 hs]
但我相信有一种更有效的方法可以做到这一点
我的预期输出如下所示:
db.getCollection('sales').aggregate([
{ "$facet": {
"00:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T00:00:00.000Z"),$lt: ISODate("2020-05-10T00:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"01:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T01:00:00.000Z"),$lt: ISODate("2020-05-10T01:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"02:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T02:00:00.000Z"),$lt: ISODate("2020-05-10T02:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
"03:00": [
{ "$match" : {createdAt: {$gte: ISODate("2020-05-10T03:00:00.000Z"),$lt: ISODate("2020-05-10T03:59:00.001Z")},seller_id: "5aa1c2c35ef7a4e97b5e995a",
}},
{ "$count": "sales" },
],
}},
{ "$project": {
"ventas0": { "$arrayElemAt": ["$01:00.sales", 0] },
"ventas1": { "$arrayElemAt": ["$02:00.sales", 0] },
"ventas3": { "$arrayElemAt": ["$03:00.sales", 0] },
}}
])
[countsale(00:00),countsale(01:00),countsale(02:00),countsale(03:00), etc to 24 hs]
你是对的,有一种更有效的方法。我们可以使用和,特别是通过使用分组 这将为您提供以下结果:
[
{
_id: 0,
count: x
},
{
_id: 1,
count: y
},
...
{
_id: 23,
count: z
}
]
从这里,您可以根据需要轻松地重新构造数据
我所看到的一个问题是没有任何匹配项的小时数(即
count=0
)将不存在于结果集中。您必须手动填补这些空白。您是对的,有一种更有效的方法可以做到这一点。我们可以使用和,特别是通过使用分组
这将为您提供以下结果:
[
{
_id: 0,
count: x
},
{
_id: 1,
count: y
},
...
{
_id: 23,
count: z
}
]
从这里,您可以根据需要轻松地重新构造数据
我所看到的一个问题是没有任何匹配项的小时数(即count=0
)将不存在于结果集中。你必须手动填补这些空白