【问题标题】:MongoDB $geoNear aggregation pipeline (using query option and using $match pipeline operation) giving different no of resultsMongoDB $geoNear 聚合管道(使用查询选项和使用 $match 管道操作)给出不同的结果
【发布时间】:2017-06-29 15:53:03
【问题描述】:

我使用 $geoNear 作为聚合框架的第一步。我需要根据“标签”字段过滤掉结果,它工作正常,但我看到有两种方法都给出不同的结果。

MongoDB 文档示例

{ “位置”: [ 40.80143, -73.96095 ], “标签”:“披萨” }

我已将 2dsphere 索引添加到“位置”键

db.restaurants.createIndex( { 'position' : "2dsphere" } )

查询 1

使用$match聚合管道操作,根据“tag”键过滤掉结果 db.restaurants.aggregate( [ { "$geoNear":{ “近”:{类型:“点”,坐标:[55.8284,-4.207]}, “限制”:100, “最大距离”:10*1000, "distanceField": "dist.calculated", "includeLocs": "dist.location", “距离乘数”:1/1000, “球形”:真 } },{ "$match":{"tag":"pizza"} }, { "$group":{"_id":null,"totalDocs":{"$sum":1}} } ] );

查询 2

在 $geoNear 聚合操作中使用查询来根据“tag”键过滤结果 db.restaurants.aggregate( [ { "$geoNear":{ “查询”:{“标签”:“披萨”} “近”:{类型:“点”,坐标:[55.8284,-4.207]}, “限制”:100, “最大距离”:10*1000, "distanceField": "dist.calculated", "includeLocs": "dist.location", “距离乘数”:1/1000, “球形”:真 } }, { "$group":{"_id":null,"totalDocs":{"$sum":1}} } ] );

分组选项只是获取两个查询返回的文档数。

两个查询返回的 totalDocs 似乎不同。

有人可以解释一下这两个查询之间的区别吗?

【问题讨论】:

    标签: mongodb geospatial 2dsphere


    【解决方案1】:

    几个假设:-
    1. 假设有 300 条基于位置匹配的记录。
    2. 假设第一组 100 个结果没有标签比萨。其余 200 个文档(101 到 300)有标签 Pizza

    查询 1:-

    • 有 2 个管道操作 $geoNear 和 $match
    • $geoNear 管道操作的输出是 $match 的输入 流水线操作
    • $geoNear 查找最多 100 个结果(我们已指定限制)基于 按最近到远距离排序的位置。 (注意这里的 返回的 100 个结果完全基于位置。所以这100 结果不包含任何带有“pizza”标签的文档)
    • 这 100 个结果被发送到下一个管道操作 $match from 过滤发生的地方。但是自从第一组100个结果 没有标签披萨,输出为空

    查询 2:-

    • 只有 1 个管道操作 $geoNear
    • $geoNear 管道操作中包含一个查询字段 $geoNear 查找最多 100 个结果(我们指定的限制)基于 按最近到远距离排序的位置和查询 标签=披萨
    • 现在从 101 到 200 的结果作为输出返回 查询包含在管道操作 $geoNear 中。所以在 我们说的简单句子,找到所有位置为 [x,y] 的文档 标签=披萨。

    P.S : - 添加 $group 管道阶段只是为了获取计数,因此没有在说明中写下它

    【讨论】:

      【解决方案2】:
      // If you have to apply multiple criteria to find locations then this query might helpful
      
       const userLocations = await userModel.aggregate([
                  {
                      $geoNear: {
                          near: { type: "Point", coordinates: [data.lon1,data.lat1] 
                            },//set the univercity points
                          spherical: true,
                          distanceField: "calcDistance",
                          // maxDistance: 2400,//25km 
                          "distanceMultiplier": 0.001,
                         
                      }
                  },
                  { $unwind: "$location" }, 
                  { $match: {
                      "location": {
                        $geoWithin: {
                          $centerSphere: [
                            [ 73.780553, 18.503327], 20/ 6378.1        //check the user point is present here
                          ]
                        }
                      }
                    }},
              
      
              ])
      

      【讨论】:

        猜你喜欢
        • 2015-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-27
        • 2021-12-06
        • 2021-07-13
        • 2020-04-13
        相关资源
        最近更新 更多