首页 > 解决方案 > 使用 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
            },
            
        ]


标签: node.jsmongodbmongooseaggregate

解决方案


你可以试试这个

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
    }
  }
])

在线运行代码

如果下次可以

  • 中的数据(提供足够的数据来测试查询)
  • 查询半成品或/并解释您想要做什么
  • 数据输出(它们应该来自数据输入)

推荐阅读