[英]$geoNear - $maxDistance in mongodb aggregation not working
我有以下查詢,它使用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聚合忽略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
}
]
請讓我知道解決方案。我的基本要求是使用給定的里程和緯度來獲得結果,並且隨着用戶的增加,里程數越來越長。
我發現為什么我總是得到100結果,因為默認情況下$geoNear
返回100結果,為什么我總是看到100結果,在增加限制后數字從100增加到134更多。 最終查詢
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,
num: 1000
}
}]).itcount()
結果數:409
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.