node.js - 使用 mongodb 计算订购次数最多的产品
问题描述
我的订单架构是这样的:
"products":[
{
"productId":"6109b819d3eb7e20a8bea1d6",
"name":"Shirt Yellow",
"salePrice":19000,
"purchasePrice":16000,
"discount": 100,
"quantity": 2,
"size":"M",
"color":"Green"
},
{
"productId":"6109b819d3eb7e20a8bea1d6",
"name":"Shirt Green",
"salePrice":19000,
"purchasePrice":16000,
"discount": 100,
"quantity": 2,
"size":"M",
"color":"Green"
}],
"storeId":"610a52f7e60d0016e4f98f63",
"storeName":"stylo",
"totalPrice":30000,
"totalPurchasePrice":25000,
"shippingFee":100,
"deliveryInstructions":"Please wear mask while delivery.",
"totalQuantity": 5
}
在这里,我想获得 5 个最订购的产品,即最常出现在 products 数组中的产品。我这样写查询:
const popularProducts = await Order.aggregate([
{
"$match": {
storeId: storeId,
status: "Delivered"
}
},
{
"$group": {
"_id": {
"productId": "$products.productId",
"productName": "$products.name",
//"quantity": {"$sum":"$products.quantity"}
},
"count": { "$sum": 1}
}
},
{
"$sort": {count: -1}
},
{
"$limit": NUM_OF_DOCUMENTS
}
])
但是,它以这种形式给出结果:
"popularProducts": [
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Yellow"
]
},
"count": 7
},
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Blue"
]
},
"count": 4
},
{
"_id": {
"productId": [
"61084b757a47bd0374b2073d",
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Red",
"Shirt Green"
]
},
"count": 3
},
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Green"
]
},
"count": 3
},
{
"_id": {
"productId": [
"61084b757a47bd0374b2073d",
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Yellow",
"Shirt Green"
]
},
"count": 1
}
]
我想要分别计算每个产品的数量,但它结合了以相同顺序放置的产品。并且还分别给出了他们的结果。我怎样才能得到这种形式的结果:
"popularProducts": [
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Yellow"
]
},
"count": 8
},
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Blue"
]
},
"count": 4
},
{
"_id": {
"productId": [
"61084b757a47bd0374b2073d",
],
"productName": [
"Shirt Red",
]
},
"count": 3
},
{
"_id": {
"productId": [
"6109b819d3eb7e20a8bea1d6"
],
"productName": [
"Shirt Green"
]
},
"count": 7
},
]
解决方案
你可以试试这个
Answers 假设您有很多商店,并且这些商店有很多订单,并且您想从一个特定的商店中查找哪些产品的销售频率更高。
Unwind 从数组 1 文档的每个成员中生成,并替换 root 使该成员成为文档,然后您可以执行您已经执行的分组/排序。
db.collection.aggregate([
{
"$match": {
"$expr": {
"$and": [
{
"$eq": [
"$storeId",
"610a52f7e60d0016e4f98f63"
]
},
{
"$eq": [
"$status",
"Delivered"
]
}
]
}
}
},
{
"$unwind": {
"path": "$products"
}
},
{
"$replaceRoot": {
"newRoot": "$products"
}
},
{
"$group": {
"_id": {
"productId": "$productId",
"productName": "$name"
},
"count": {
"$sum": "$quantity"
}
}
},
{
"$sort": {
"count": -1
}
}
])
如果下次可以
- 中的数据(提供足够的数据来测试查询)
- 查询半成品或/并解释您想要做什么
- 数据输出(它们应该来自数据输入)
推荐阅读
- c# - 如何将 Hub 内容从任何服务器带到*另一台服务器*项目 Hub?
- python - matplotlib 中的 figsize 不会改变图形大小?
- r - 如何使用字符串连接定义聚集函数的关键参数
- android - Android 应用程序:如何向 Surface 显示连续的 RGBA 字节 [] 帧?
- html - 如何使导航栏中的引导按钮跨越整个导航栏
- python - 用于计算python中间歇croston方法的拟合值的用户定义函数
- c# - 这是处理中间件身份验证重定向逻辑的好方法吗?
- node.js - 使用 mongoose 连接到 MongoDb Atlas
- .net - Asp.Net 声称在哪里成立?
- css - 使用 SCSS 中的 Webpack 编译后,媒体查询不起作用