【问题标题】:MongoDB query filterMongoDB 查询过滤器
【发布时间】:2021-03-19 08:41:06
【问题描述】:

我收藏了餐厅、产品和评论

restaurants: [
    {
      _id: 1,
      name: "Burger King",
      location: {
        type: "Point",
        coordinates: [
          11.111,
          11.111
        ]
      },
      isOpen: true
    },
    {
      _id: 2,
      name: "McDonald's",
      location: {
        type: "Point",
        coordinates: [
          22.222,
          22.222
        ]
      },
      isOpen: true
    },
    {
      _id: 3,
      name: "Chick-fil-A",
      location: {
        type: "Point",
        coordinates: [
          33.333,
          33.333
        ]
      },
      isOpen: true
    }
  ],
products: [
    {
      _id: 1,
      name: "Breakfast Whopper Jr.",
      price: "$1.29",
      isAvailable: true,
      isApproved: true,
      quantitySold: 50,
      restaurant: ObjectId("1")
      
    },
    {
      _id: 2,
      name: "Big Mac",
      price: "$4.35",
      isAvailable: true,
      isApproved: true,
      quantitySold: 59,
      restaurant: ObjectId("2")
    },
    {
      _id: 3,
      name: "Spicy Chicken Sandwich",
      price: "$3.29",
      isAvailable: true,
      isApproved: true,
      quantitySold: 60,
      restaurant: ObjectId("3")
    },
    {
      _id: 4,
      name: "Chicken Sandwich",
      price: "$2.29",
      isAvailable: true,
      isApproved: true,
      quantitySold: 58,
      restaurant: ObjectId("3")
    }
  ],
reviews: [
    {
      _id: 1,
      message: "Big burger even if it's junior size.",
      restaurant: ObjectId("1"),
      product: ObjectId("1")
    },
    {
      _id: 2,
      message: "Big Mac is really the best burger in town.",
      restaurant: ObjectId("2"),
      product: ObjectId("2")
      
    },
    {
      _id: 3,
      message: "Spicy Chicken Sandwich rocks!",
      restaurant: ObjectId("3"),
      product: ObjectId("3")
    },
    {
      _id: 4,
      message: "Chicken Sandwich is the best sandwich of Chick-fil-A!",
      restaurant: ObjectId("3"),
      product: ObjectId("4")
    },
    {
      _id: 5,
      message: "Chicken Sandwich is the best!",
      restaurant: ObjectId("3"),
      product: ObjectId("4")
    }
  ]

我的实现

db.products.aggregate([
    { 
        $lookup: { 
            "from": "restaurant", 
            "localField": "restaurant", 
            "foreignField": "_id", 
            "as": "restaurant" 
        } 
    },
    {
        $match: {
            "restaurant.isOpen": true,
            "isApproved": true,
            "isAvailable": true
        }
    },
    {
        $project: {
            "restaurant.isOpen": 1,
            "isApproved": 1,
            "isAvailable": 1,
            "restaurant.location": 1,
            "quantitySold": 1
        }
    },
    {
        $match: {
            "restaurant.location": {
                $geoWithin: {
                    $centerSphere: [[222.22, 222.33], 10/6378.1] // sample coordinates
                }
            }
        }
    },
    {
        $sort: {
            // best seller
            "quantitySold": -1
        }
    }

在我的实现中。我目前正在获得 isOpen: true 及其产品 isAvailable: trueisApproved: true 的 10 公里餐厅。我还按 quantitySold 字段对最畅销的商品进行了排序。

现在,我还想查询评论最多的产品,并且我想根据畅销书和评论最多的产品在我的应用中仅显示每家餐厅的 1 件产品。然后,剩下的产品将随机排列在最畅销和评论最多的下面。

示例。我在汉堡王、MCDO、Chick-fil-A 的 10 公里处,我会根据畅销书和评论最多的产品(1 个产品)查看他们的产品。然后,我会在下面看到他们所有的产品随机化。

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    目前您有 OPEN(餐厅)、APPROVED 和 AVAILABLE 项目列表。 (您必须调整现有查询的 $project 部分以相应地获得更多字段)。

    要获取评论,您可以使用 $lookup,其中

    {
         $lookup:
           {
             from: "reviews",
             localField: "_id",
             foreignField: "product",
             as: "product_reviews"
           }
      }
    

    您将获得每个产品的 product_reviews 数组。然后,您可以执行 $groupby 和 $count 以获取下一个管道阶段中每个产品的总计数。

    在聚合管道中获取上述列表后,使用 GROUP BY 如下

    { $组:{ _id : "$restaurant_id", 项目:{ $push : "$$ROOT" } }}

    (在现有查询的 $project 阶段获取产品的餐厅 ID 和名称)。现在您有了一个包含餐厅 ID/名称及其产品数组的列表。 您可以参考以下链接了解更多关于 $$ROOT 的信息

    Mongo group and push: pushing all fields

    由于您的列表已经排序,因此最畅销的商品将位于顶部。

    关于其他食物,你真的需要每次都随机化吗?然后您可以尝试使用 $sample(请参阅:https://docs.mongodb.com/manual/reference/operator/aggregation/sample/) 或根据您的需要关注此mongo db aggregate randomize ( shuffle ) results

    【讨论】:

    • 我怎样才能得到我的评论集合谁拥有最多评论的产品?
    • 要获得评论,您应该在初始聚合查询中使用 $lookup。
    • 我有 3 个字段。我已经在查找我的产品和餐厅。
    猜你喜欢
    • 2019-11-24
    • 2012-03-27
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多