【问题标题】:Mongo query to find top store from list of ordersMongo查询从订单列表中查找顶级商店
【发布时间】:2021-12-17 14:00:46
【问题描述】:

我对 Mongo 还是很陌生。我有两个集合,如下所示。

订单收集

[
    {
      id: 1,
      price: 249,
      store: 1,
      status: true
    },
    {
      id: 2,
      price: 230,
      store: 1,
      status: true
    },
    {
      id: 3,
      price: 240,
      store: 1,
      status: true
    },
    {
      id: 4,
      price: 100,
      store: 2,
      status: true
    },
    {
      id: 5,
      price: 150,
      store: 2,
      status: true
    },
    {
      id: 6,
      price: 500,
      store: 3,
      status: true
    },
    {
      id: 7,
      price: 70,
      store: 4,
      status: true
    },
    
  ]

商店收藏

[
    {
      id: 1,
      name: "Store A",
      status: true
    },
    {
      id: 2,
      name: "Store B",
      status: true
    },
    {
      id: 3,
      name: "Store C",
      status: true
    },
    {
      id: 4,
      name: "Store D",
      status: false
    }
  ]

如何从订单列表中找到排名靠前的店铺,应该根据每家店铺的总销售额。

我尝试过以下方法

db.order.aggregate([
  {
    "$match": {
      status: true
    }
  },
  {
    "$group": {
      "_id": "$store",
      "totalSale": {
        "$sum": "$price"
      }
    }
  },
  {
    $sort: {
      totoalSale: -1
    }
  }
])

我从上面的 sn-ps 中得到了商店的排序列表。但我想在总销售额的同时添加商店详细信息。

更多:https://mongoplayground.net/p/V3UH1r6YRnS

预期输出

[
  {
    id: 1,
    name: "Store A",
    status: true,
    totalSale: 719
  },
  {
    id: 1,
    name: "Store c",
    status: true,
    totalSale: 500
  },
  {
    _id: 2,
    id: 1,
    name: "Store B",
    status: true,
    totalSale: 250
  },
  {
    _id: 4,
    name: "Store D",
    status: true,
    totalSale: 70
  }
]

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:
    1. $lookup - store 集合加入 order 集合并生成新字段 store_orders
    2. $set - 使用来自store_ordersstatus: true 过滤order
    3. $set - totalSale store_orders.price 的字段总和。
    4. $sort - 按降序对totalSale 进行排序。
    5. $unset - 删除 store_orders 字段。
    db.store.aggregate([
      {
        $lookup: {
          from: "order",
          localField: "id",
          foreignField: "store",
          as: "store_orders"
        }
      },
      {
        $set: {
          "store_orders": {
            $filter: {
              input: "$store_orders",
              as: "order",
              cond: {
                $eq: [
                  "$$order.status",
                  true
                ]
              }
            }
          }
        }
      },
      {
        $set: {
          "totalSale": {
            "$sum": "$store_orders.price"
          }
        }
      },
      {
        $sort: {
          totalSale: -1
        }
      },
      {
        $unset: "store_orders"
      }
    ])
    

    Sample Mongo Playground

    【讨论】:

      【解决方案2】:

      你可以从store集合、$lookuporder集合、$sumtotalSales开始,然后争吵到你想要的形式

      db.store.aggregate([
        {
          "$lookup": {
            "from": "order",
            let: {
              id: "$id"
            },
            pipeline: [
              {
                $match: {
                  $expr: {
                    $eq: [
                      "$$id",
                      "$store"
                    ]
                  }
                }
              },
              {
                $group: {
                  _id: null,
                  totalSale: {
                    $sum: "$price"
                  }
                }
              }
            ],
            "as": "totalSale"
          }
        },
        {
          $unwind: "$totalSale"
        },
        {
          $addFields: {
            totalSale: "$totalSale.totalSale"
          }
        },
        {
          $sort: {
            totalSale: -1
          }
        }
      ])
      

      这里是Mongo playground 供您参考。

      【讨论】:

        猜你喜欢
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-07
        相关资源
        最近更新 更多