MongoDB $ geoNear聚合管道(使用查询选项和$ match管道操作)给出不同的结果数 [英] MongoDB $geoNear aggregation pipeline (using query option and using $match pipeline operation) giving different no of results

查看:554
本文介绍了MongoDB $ geoNear聚合管道(使用查询选项和$ match管道操作)给出不同的结果数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用$ geoNear作为聚合框架的第一步.我需要根据标签"字段过滤出结果,并且效果很好,但我发现有两种方法都能给出不同的结果.

I am using a $geoNear as the first step in the aggregation framework. I need to filter out the results based on "tag" field and it works fine but I see there are 2 ways both giving different results.

示例MongoDB文档



    {
      "position": [
        40.80143,
        -73.96095
      ],
      "tag": "pizza"
    }

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

I have added 2dsphere index to the "position" key



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

查询1



    db.restaurants.aggregate(
      [
       {
           "$geoNear":{

               "near": { type: "Point", coordinates: [ 55.8284,-4.207] },
               "limit":100,
               "maxDistance":10*1000,
               "distanceField": "dist.calculated",
               "includeLocs": "dist.location",
               "distanceMultiplier":1/1000,
               "spherical": true
        }
       },{
           "$match":{"tag":"pizza"}
       },

       {
          "$group":{"_id":null,"totalDocs":{"$sum":1}}
       }
      ]
    );

查询2



    db.restaurants.aggregate(
      [
       {
           "$geoNear":{
               "query" : {"tag":"pizza"}
               "near": { type: "Point", coordinates: [ 55.8284,-4.207] },
               "limit":100,
               "maxDistance":10*1000,
               "distanceField": "dist.calculated",
               "includeLocs": "dist.location",
               "distanceMultiplier":1/1000,
               "spherical": true
        }
       },
       {
          "$group":{"_id":null,"totalDocs":{"$sum":1}}
       }
      ]
    );

分组选项仅用于获取两个查询返回的文档数.

The grouping option is just to get the count of documents returned by both the queries.

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

The totalDocs returned by both queries seem to be different.

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

Can someone explain me the differences between both the queries ?

推荐答案

一些假设:-
1.假设有300条根据位置匹配的记录.
2.假设第一组100个结果中没有标签Pizza.其余200个文档(101到300个)带有标签披萨

Few assumptions:-
1. Assume there are 300 records that match based on the location.
2. Assume first set of 100 results do not have tag pizza. The rest 200 documents (101 to 300) have tag pizza

查询1:-

  • 有2个管道操作$ geoNear和$ match
  • $ geoNear管道操作的输出是$ match的输入 管道操作
  • $ geoNear根据以下条件最多可找到100个结果(我们指定的限制) 位置按最近到很远的距离排序. (请注意, 记录的100个结果完全基于位置.所以这100 结果不包含任何带有标签"pizza"的文档)
  • 这100个结果将发送到下一个管道操作$ match 进行过滤的位置.但是自从第一组100个结果以来 没有标签披萨,输出为空
  • There are 2 pipeline operations $geoNear and $match
  • The output of $geoNear pipeline operation is the input to $match pipeline operation
  • $geoNear finds max of 100 results (limit we have specified) based on the location sorted by nearest to far distance. (Note here that the 100 results retured are purely based on the location. So these 100 results do not contain any document with tag "pizza")
  • These 100 results are sent to the next pipeline operation $match from where the filtering happens. But since the first set of 100 results did not have tag pizza, the output is empty

查询2:-

  • 只有1个管道操作$ geoNear
  • $ geoNear管道操作中包含一个查询字段 $ geoNear根据以下内容最多可找到100个结果(我们指定的限制) 根据最近到很远的距离和查询排序的位置 tag = pizza
  • 现在这里将101到200的结果作为输出返回 查询包含在管道操作$ geoNear中.所以在 我们说一个简单的句子,找到位置为[x,y]的所有文档 tag = pizza.
  • There is only 1 pipeline operation $geoNear
  • There is a query field included in the $geoNear pipeline operation $geoNear finds max of 100 results (limit we have specified) based on the location sorted by nearest to far distance and the query tag=pizza
  • Now here the results from 101 to 200 are returned as output as the query is included within the pipeline operation $geoNear. So in simple sentence we say, find all documents with location [x,y] with tag=pizza.

P.S:-添加$ group管道阶段只是为了获得计数,因此在解释中没有写过

P.S : - The $group pipeline stage is added just for getting the count and hence have not written about it in the explaination

这篇关于MongoDB $ geoNear聚合管道(使用查询选项和$ match管道操作)给出不同的结果数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆