MongoDB 查询以从订单列表中查找排名靠前的商店

MongoDB query to find top store from list of orders

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

我从上面的代码片段中得到了经过排序的商店列表。但我想添加商店详细信息以及总销售额。

更多: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
  }
]

您可以从 store 集合开始,$lookup order 集合,$sum totalSales,然后整理成您期望的形式

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供您参考。

  1. $lookup - store 集合加入 order 集合并生成新字段 store_orders.
  2. $set - 使用来自 store_orders.
  3. status: true 过滤 order
  4. $set - totalSale store_orders.price.
  5. 的字段总和
  6. $sort - 按降序排列 totalSale
  7. $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