我在聚合框架中使用了$geoNear作为第一步骤。我需要基于“tag”字段过滤结果,它可以工作得很好,但我发现有两种方式都能够给出不同的结果。
MongoDB示例文档:
{ "position": [ 40.80143, -73.96095 ], "tag": "pizza" }
我已经将2dsphere索引添加到“position”键。
db.restaurants.createIndex( { 'position' : "2dsphere" } )
查询1:使用$match操作筛选出基于“tag”键的结果
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:在$geoNear聚合操作内使用查询来基于“tag”键过滤结果
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}} } ] );
这里的分组选项只是为了获取两个查询返回的文档计数。
两个查询返回的totalDocs似乎不同。有人能解释一下这两个查询之间的差异吗?
MongoDB示例文档:
{ "position": [ 40.80143, -73.96095 ], "tag": "pizza" }
我已经将2dsphere索引添加到“position”键。
db.restaurants.createIndex( { 'position' : "2dsphere" } )
查询1:使用$match操作筛选出基于“tag”键的结果
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:在$geoNear聚合操作内使用查询来基于“tag”键过滤结果
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}} } ] );
这里的分组选项只是为了获取两个查询返回的文档计数。
两个查询返回的totalDocs似乎不同。有人能解释一下这两个查询之间的差异吗?