【发布时间】:2022-11-12 00:42:37
【问题描述】:
架构:
const orderSchema = mongoose.Schema(
{
orderStatus: {
type: String,
enum: ["pending", "preparing", "completed", "declined"],
default: "pending",
},
products: [
{
product: {
productId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Product",
},
productName: String,
productPrice: Number,
categoryName: String,
},
quantity: {
type: Number,
required: true,
}
},
],
totalPrice: { type: Number },
acceptDeclineTime: {
type: Date,
default: Date.now,
},
}
);
我想要一份年度销售报告,其中包含接受和拒绝的订单数量,以及每个订单的总价。
我试过了:
orderSchema.aggregate(
[
{
$unwind: {
path: "$products",
},
},
{
$group: {
_id: { $year: { date: "$acceptDeclineTime", timezone: "+03:00" } },
totalCompletedPrice: {
$sum: {
$cond: [{ $eq: ["$orderStatus", "completed"] }, "$totalPrice", 0],
},
},
totalDeclinedPrice: {
$sum: {
$cond: [{ $eq: ["$orderStatus", "declined"] }, "$totalPrice", 0],
},
},
totalItems: {
$sum: "$products.quantity",
},
completedSales: {
$sum: {
$cond: [{ $eq: ["$orderStatus", "completed"] }, "$products.quantity", 0],
},
},
cancelledSales: {
$sum: {
$cond: [{ $eq: ["$orderStatus", "declined"] }, "$products.quantity", 0],
},
},
},
},
]);
但是价格计算是错误的,因为$unwind 阶段重复了产品的总价格,这将在$sum 操作上出现问题。
【问题讨论】:
-
您可以编写一个聚合管道,它将根据来自 acceptDeclineTime 的年份值对条目进行分组。您可以在 $facet 运算符上查找以将结果分为接受和拒绝。确保您在日期字段上有正确的索引
-
请检查我上面的问题,因为我已经添加了我要使用的路径
-
也许代替
"$totalPrice"使用{$multiply: ["$products.productPrice", "$products.quantity"]} -
@WernfriedDomscheit productPrice 有时可能不包含餐厅设置的增值税和服务费,而 totalPrice 包含
标签: javascript node.js mongodb mongoose