Mongodb + GeoNear + Aggregation + Large Collection

204 views
Skip to first unread message

Fausto Torres

unread,
Aug 17, 2017, 3:32:02 PM8/17/17
to mongodb-user
I got a limitation well know that haven't been solved by mongodb, where I mixed aggregation with geonear in a very large collection. The problem is by default it returns only 100 first results , being unable to go through others results. Always I have to go through the code and change limit to go further.

Is there a way to solve it ?

Kevin Adistambha

unread,
Aug 18, 2017, 12:48:43 AM8/18/17
to mongodb-user

Hi Fausto

The problem is by default it returns only 100 first results, being unable to go through others results

The aggregation $geoNear stage actually allows you to increase this default using the limit parameter. For example:

db.geo.aggregate([{$geoNear:{near:{type:"Point", coordinates:[151.209264, -33.883711]}, distanceField:"d", spherical:true, maxDistance:1000, num:500}}])

The above query will return at most 500 results within 1 km of the given coordinate. Please note that increasing the default limit of 100 results may have an impact on performance (depending on your setup), since $geoNear returns the result in order of distance.

If you don’t need the result to be ordered, another alternative is to use $centerSphere, which may be more performant for your use case.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages