Reputation: 207
I have the following query which return the results based on the longitude and latitude using "2dsphere
" index of mongoDB find(),however when i use the same query in aggregation the $maxDistance
is not working and always showing the same result.
db.travelLocationSearch.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-3.7037901999999576, 40.4167754] // madrid, spain
},
$maxDistance: (60*1609.344) // miles to meters
}
}
}).count()
Result count on 60 miles - 147
on 200 miles - 170
on 500 miles - 671
However when i write same query using mongo aggregate
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()
Result count on 60 miles - 100
on 200 miles - 100
on 500 miles - 100
I also verify my query as explained here ($geoNear aggregation ignoring maxDistance)
Below is my index
> 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
}
]
Please let me know the solution.My basic requirement is getting the results using given miles and lat, long and as user increases the miles result get longer and longer.
Upvotes: 4
Views: 3153
Reputation: 207
I found that why i am getting the 100 result always, because by default the $geoNear
returned 100 result that why i am seeing always 100 result, after increasing the limit the number are getting higher from 100 to 134 to more.
final query
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()
Result count: 409
Upvotes: 4