我需要帮助。我想修复此代码。我已经尝试了几个小时。我想获取“每天售出的订单-默认日期范围:过去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
}
]
答案 0 :(得分:0)
保留原始结构,并使用$$ROOT变量保留订单数据:
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"},
orders: {$push: "$$ROOT"}
}
}
]);
编辑:
由于我们不知道数据库中的最后日期,因此您可以使用打击聚合来获得所需的结果,因此我们必须首先对所有集合进行分组。
我个人建议您不要这样做,而是将其分为2个步骤,首先查询以查找最大日期。那么您可以使用
{
$match: {
"datetime": {
$lte: (new Date((maxDateFromCollection.getTime() - (30 * 24 * 60 * 60 * 1000)))
}
}
}
进行匹配,并且当比例尺变大时,效率会提高很多。
connection.db.collection("orders").aggregate([
{
"$group": {
_id: {
"year": {"$year": "$datetime"},
"month": {"$month": "$datetime"},
"day": {"$dayOfMonth": "$datetime"}
},
totalValue: {$sum: "$subtotal"},
}
},
{
$sort: {
"_id.year": -1,
"_id.month": -1,
"_id.day": -1,
}
},
{
$limit: 30
},
// next steps are only necessary if days could be missing and we have less than 30 results.
{
$group: {
_id: null,
orders: {$push: "$$ROOT"},
maxDate: {$first: "$_id"}
}
},
{
$unwind: "$orders"
},
{
$match: {
$or: [
{
$and: [ // same month if its months with 30 or less days. or day greater than 1.
{
$or: [
{
"orders._id.day": {$gt: 1},
},
{
"orders._id.month": {$in: [2, 4, 6, 8, 10, 12]}
}
]
},
{
$expr: {
$eq: ["$orders._id.year", "$maxDate.year"]
}
},
{
$expr: {
$eq: ["$orders._id.month", "$maxDate.month"]
}
}
]
},
{
$and: [ // if day is 1 than no 31
{
$expr: {
$eq: [
"$orders._id.year", "$maxDate.year"]
}
},
{
$expr: {
$eq: [
"$orders._id.month", "$maxDate.month"]
}
},
{
"orders._id.day": {$ne: 31}
}
]
},
{ // days from previous month
$and: [
{
"maxDate.month": {$in: [5, 7, 10, 12]}
},
{
$expr: {
$eq: [
"$orders._id.year", "$maxDate.year"]
}
},
{
$expr: {
$eq: [
"$orders._id.month", {$subtract: ["$maxDate.month", 1]}
]
}
},
{
$expr: {
$gt: [
"$orders._id.day", "$maxDate.day"
]
}
}
]
},
{ // days from previous month
$and: [
{
"maxDate.month": {$in: [2, 4, 6, 8, 9, 11]}
},
{
$expr: {
$eq: [
"$orders._id.year", "$maxDate.year"
]
}
},
{
$expr: {
$eq: [
"$orders._id.month", {$subtract: ["$maxDate.month", 1]}
]
}
},
{
$expr: {
$gt: [
"$orders._id.day", {$add: ["$maxDate.day", 1]}
]
}
}
]
},
{ // days from previous month
$and: [
{
"maxDate.month": 3
},
{
$expr: {
$eq: [
"$orders._id.year", "$maxDate.year"
]
}
},
{
$expr: {
$eq: [
"$orders._id.month", {$subtract: ["$maxDate.month", 1]}
]
}
},
{
$expr: {
$gt: [
"$orders._id.day", {$subtract: ["$maxDate.day", 2]}
]
}
}
]
},
{ // days from previous month
$and: [
{
"maxDate.month": 1
},
{
"orders._id.month": 12
},
{
$expr: {
$eq: [
"$orders._id.year", {$subtract: ["$maxDate.year", 1]}
]
}
},
{
$expr: {
$gt: [
"$orders._id.day", {$add: ["$maxDate.day", 1]}
]
}
}
]
},
]
}
},
{
$replaceRoot: {
newRoot: "$orders"
}
}
])