Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB查询过滤器_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

MongoDB查询过滤器

MongoDB查询过滤器,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我收集了餐厅、产品和评论 restaurants: [ { _id: 1, name: "Burger King", location: { type: "Point", coordinates: [ 11.111, 11.111 ] }, isOpen: true }, {

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

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
        }
    }
在我的实现中。我目前正在获得10公里长的isOpen:true餐厅及其可提供的产品:true和获得批准的产品:true。我还按QuantitySeld字段对畅销书进行了排序

现在,我还想查询评论最多的产品,我想根据畅销书和评论最多的产品,在我的应用程序中,每个餐厅只显示一种产品。然后,在这之后,剩余的产品将随机排列在排序后的畅销书和评论最多的产品下面

示例。我在汉堡王、麦当劳、奇克-菲尔-A的10公里处,我将看到他们基于畅销书和评论最多的产品(1个产品)。然后,我将看到他们所有的产品随机如下

目前,您拥有已开业(餐厅)、已批准且可用的物品清单。 (您必须调整现有查询的$project部分,以相应地获得更多字段)

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

{
     $lookup:
       {
         from: "reviews",
         localField: "_id",
         foreignField: "product",
         as: "product_reviews"
       }
  }
对于每种产品,您将获得product_reviews数组。然后,您可以执行$groupby和$count,以获取下一管道阶段中每个产品的总计数

在聚合管道中获取上述列表后,使用分组方式,如下所示

{$group:{ _id:“$restaurant_id”, 项目:{$push:$$ROOT} }}

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

由于您的列表已经排序,您将有最畅销的项目在顶部

关于其他食物,你真的需要每次都随机化吗?然后,您可以尝试使用$sample(请参阅:)
或者根据您的需要执行此操作。

如何获取评论产品最多的人的评论集合?要获取评论,您应该在初始聚合查询中使用$lookup。我有3个字段。我已经在查我的产品和餐馆了。