Мне нужна помощь. Я хочу исправить этот код. Я пытался часами. Я хочу получить "заказы, проданные за день - диапазон дат по умолчанию: последние 30 дней".
Вот как выглядит коллекция заказов
{
status: 'BEING_PREPARED',
option: 'DELIVERY',
driver: null,
staffComment: '',
driverComment: null,
paymentMethod: null,
etd: null,
ptd: null,
deliveryFees: 0,
discount: 0,
voucherId: null,
orderItems: [[Object], [Object], [Object]],
subtotal: 24.849999999999998,
branch: ObjectId("5c98a67a2061ed07b45100e1"),
datetime: ISODate("2019-04-06T17:11:43.171Z"),
number: 1347,
}
Это то, что я сделал так далеко.
router.route('/api/ordes')
.get(function(req, res) {
var result = connection.db.collection("orders").aggregate([{
$match: {
"datetime": {
$lte: (new Date((new Date()).getTime() - (30 * 24 * 60 * 60 * 1000)))
}
}
},
{
"$group": {
_id: {
"year": { "$year": "$datetime" },
"month": { "$month": "$datetime" },
"day": { "$dayOfMonth": "$datetime" }
},
totalValue: { $sum: "$subtotal" }
}
}
]);
result.toArray(function(err, doc) {
console.log(doc);
res.status(200).json(doc);
});
});
Это результаты, которые я получил
[
{
"_id": {"year": 2019, "month": 7, "day": 2}, "totalValue": 3875.92
},
{
"_id": {"year": 2019, "month": 6, "day": 30}, "totalValue": 9594.92
},
{
"_id": {"year": 2019, "month": 6, "day": 29}, "totalValue": 8820.369999999999
},
{
"_id": {"year": 2019, "month": 6, "day": 28}, "totalValue": 8881.76
},
{
"_id": {"year": 2019, "month": 6, "day": 27}, "totalValue": 4286.66
},
{
"_id": {"year": 2019, "month": 6, "day": 26}, "totalValue": 4716.66
},
{
"_id": {"year": 2019, "month": 6, "day": 25}, "totalValue": 3093.82
},
{
"_id": {"year": 2019, "month": 6, "day": 23}, "totalValue": 9905.26
},
{
"_id": {"year": 2019, "month": 6, "day": 22}, "totalValue": 10492.32
},
{
"_id": {"year": 2019, "month": 6, "day": 21}, "totalValue": 9261.52
},
{
"_id": {"year": 2019, "month": 6, "day": 20}, "totalValue": 3794.98
},
{
"_id": {"year": 2019, "month": 6, "day": 19}, "totalValue": 4191.64
},
{
"_id": {"year": 2019, "month": 6, "day": 17}, "totalValue": 3749.33
},
{
"_id": {"year": 2019, "month": 6, "day": 4}, "totalValue": 1915.72
},
{
"_id": {"year": 2019, "month": 6, "day": 2}, "totalValue": 5884.91
},
{
"_id": {"year": 2019, "month": 5, "day": 29}, "totalValue": 2784.5
},
{
"_id": {"year": 2019, "month": 5, "day": 28}, "totalValue": 1986.87
},
{
"_id": {"year": 2019, "month": 5, "day": 26}, "totalValue": 7209.74
},
{
"_id": {"year": 2019, "month": 5, "day": 24}, "totalValue": 5398.99
},
{
"_id": {"year": 2019, "month": 7, "day": 4}, "totalValue": 4256.6
},
{
"_id": {"year": 2019, "month": 5, "day": 23}, "totalValue": 2365.48
},
{
"_id": {"year": 2019, "month": 5, "day": 22}, "totalValue": 2452.8199999999997
}
]