我使用$ geoNear作为聚合框架的第一步 . 我需要根据“标签”字段过滤掉结果并且它工作正常,但我发现有两种方法都能给出不同的结果 .
Sample MongoDB Document
{
"position": [
40.80143,
-73.96095
],
"tag": "pizza"
}
我在“位置”键中添加了2dsphere索引
db.restaurants.createIndex( { 'position' : "2dsphere" } )
Query 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}}
}
]
);
Query 2
Uses query inside the $geoNear aggregation operation to filter results based on "tag" key
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似乎不同 .
有人可以解释我两个查询之间的差异吗?