【发布时间】:2017-07-18 23:22:41
【问题描述】:
我有以下查询,它使用 mongoDB find() 的“2dsphere”索引根据经度和纬度返回结果,但是当我在聚合中使用相同的查询时,$maxDistance 不起作用并且总是显示同样的结果。
db.travelLocationSearch.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-3.7037901999999576, 40.4167754] // madrid, spain
},
$maxDistance: (60*1609.344) // miles to meters
}
}
}).count()
60 英里的结果计数 - 147
200 英里 - 170
500 英里 - 671
但是,当我使用 mongo 聚合编写相同的查询时
db.travelLocationSearch.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -3.7037901999999576, 40.4167754 ] },
distanceField: "dist.calculated",
maxDistance: (100 * 1609.34), // miles to meter
distanceMultiplier: 0.000621371, // meter to miles
includeLocs: "dist.location",
spherical: true
}
}
]).itcount()
60 英里的结果计数 - 100
200 英里 - 100
500 英里 - 100
我还按照此处的说明验证了我的查询 ($geoNear aggregation ignoring maxDistance)
以下是我的索引
> db.travelLocationSearch1.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "zen.travelLocationSearch"
},
{
"v" : 2,
"key" : {
"location" : "2dsphere"
},
"name" : "location_2dsphere",
"ns" : "zenbrisa.travelLocationSearch1",
"2dsphereIndexVersion" : 3
}
]
请让我知道解决方案。我的基本要求是使用给定的里程和纬度、经度获得结果,并且随着用户增加里程结果会越来越长。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework geospatial