Node.js 基于分组的$sum在sum字段的mongodb rathar重新分组中未获得正确的sum?
我有几个系列,我想从中获得前六大畅销品牌 收藏品: //这就是mongodb atlas上的外观 收款顺序: //这就是mongodb atlas上的外观 收款单\产品\枢轴: //这就是mongodb atlas上的外观 品牌系列: 这是我为这个问题编写的代码,但是它基于sum字段重新组合,而不是给我sumNode.js 基于分组的$sum在sum字段的mongodb rathar重新分组中未获得正确的sum?,node.js,mongodb,express,mongoose,aggregation-framework,Node.js,Mongodb,Express,Mongoose,Aggregation Framework,我有几个系列,我想从中获得前六大畅销品牌 收藏品: //这就是mongodb atlas上的外观 收款顺序: //这就是mongodb atlas上的外观 收款单\产品\枢轴: //这就是mongodb atlas上的外观 品牌系列: 这是我为这个问题编写的代码,但是它基于sum字段重新组合,而不是给我sum const join = OrderProductPivot.aggregate([ { $lookup: { from:
const join = OrderProductPivot.aggregate([
{
$lookup: {
from: "products",
localField: "productId",
foreignField: "_id",
as: "TheProduct"
}
},
{
$unwind: "$TheProduct"
},
{
$lookup: {
from: "brands",
localField: "TheProduct.belongsToBrand",
foreignField: "_id",
as: "TheBrand"
}
},
{
$unwind: "$TheBrand"
},
{
$project: {
'_id': 1,
'TheProduct.belongsToBrand': 1,
'TheBrand.name': 1,
'quantityPurchased': 1,
'productId': 1,
// 'shortDescription': 1,
// 'createdAt': 1,
// 'AddedBy.username': 1,
}
},
{
$group: {
_id: {
"_id": "$TheProduct.belongsToBrand",
"name": "$TheBrand.name",
"total": {
$sum: "$quantityPurchased"
},
},
products: { $push: "$$ROOT" }
}
}
]);
join.exec((err, res) => {
if (err) return callback({
status: process.env.SERVER_ERROR_CODE,
message: err.message
});
callback({
status: process.env.SUCCESS_CODE,
data: res
});
});
我现在得到的结果示例:
{
"status": "200",
"data": [
{
"_id": {
"_id": null,
"total": {
"$numberDecimal": "2"
}
},
"products": [
{
"_id": "5f5a2d1e6130d73820358a81",
"productId": "5f5266b37f32df29c5f0488e",
"quantityPurchased": {
"$numberDecimal": "2"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
},
{
"_id": "5f5a2d1e6130d73820358a82",
"productId": "5f5266b37f32df29c5f0488f",
"quantityPurchased": {
"$numberDecimal": "2"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
}
]
},
{
"_id": {
"_id": null,
"total": {
"$numberDecimal": "1"
}
},
"products": [
{
"_id": "5f5a24bf6130d73820358a74",
"productId": "5f57d8632bdc2470827c12f2",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a24bf6130d73820358a75",
"productId": "5f58a96a42fe8c168e4e47d8",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a24bf6130d73820358a76",
"productId": "5f58bd6142fe8c168e4e47f7",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
},
{
"_id": "5f5a24bf6130d73820358a77",
"productId": "5f58c1f442fe8c168e4e47fc",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
},
{
"_id": "5f5a2ca96130d73820358a7a",
"productId": "5f58a96a42fe8c168e4e47d8",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ca96130d73820358a7b",
"productId": "5f57d8632bdc2470827c12f2",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ca96130d73820358a7c",
"productId": "5f58aa5e42fe8c168e4e47d9",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ca96130d73820358a7d",
"productId": "5f58ab9642fe8c168e4e47da",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ca96130d73820358a7e",
"productId": "5f58af2742fe8c168e4e47db",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ca96130d73820358a7f",
"productId": "5f58b97842fe8c168e4e47f2",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a83",
"productId": "5f5283e5bb2e474606413c51",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a84",
"productId": "5f528711bb2e474606413d0b",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a85",
"productId": "5f5283e1bb2e474606413c50",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a86",
"productId": "5f52874abb2e474606413d19",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a87",
"productId": "5f52874abb2e474606413d32",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2d1e6130d73820358a88",
"productId": "5f52874abb2e474606413d33",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f463416cfd44876a5e96f5e"
},
"TheBrand": {
"name": "Samsung"
}
},
{
"_id": "5f5a2ddb6130d73820358a8a",
"productId": "5f5266b37f32df29c5f0488e",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
},
{
"_id": "5f5a2ddb6130d73820358a8b",
"productId": "5f5266b37f32df29c5f0488f",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
},
{
"_id": "5f5a2e776130d73820358a8d",
"productId": "5f5266b37f32df29c5f0488e",
"quantityPurchased": {
"$numberDecimal": "1"
},
"TheProduct": {
"belongsToBrand": "5f4651532f6fae13ac5d8a18"
},
"TheBrand": {
"name": "Apple"
}
}
]
}
]
}
如果您提供在mongo shell中创建数据的示例insert命令,然后在您尝试使用的mongo shell中提供聚合命令,那么这个问题将更容易理解。并删除与mongoose/js有关的所有内容。我正在尝试按组获取和,但不是获取和。我的聚合方法是基于重新分组的在property QuantityPurchased上,
total
字段不应位于$group
阶段的\U id
内。谢谢@Joe,请将您的评论作为答案发布,以便我检查。这对我有用