2

Initially I wanted to support mongo db text search with geo $near functionality. Later realized that these two cannot be used together as both require indexes.

Later I've decided to use $geoWithin instead. However, the results are not sorted by distance like $near does.

I would like to know if there is any way to sort the results returned by $geoWithin by 'distance'?

Yasitha Waduge
  • 13,180
  • 5
  • 35
  • 42
  • 1
    You cannot. Nor would it make any sense. Operators like `$near` and `$text` are meant to both basically *"find things nearest like I asked"*, in either geographically near or "like" the terms entered by relevance. These cannot be used together in one query for a number of reasons, most notably because the engine processes them differently. But it generally does not make much sense, and you acutally really want one or the other. `$geoWithin` of course does not sort, and this is by design. – Blakes Seven Mar 15 '16 at 00:45
  • 1
    More lengthy response on the related [Using full text search with geospatial index on Mongodb](http://stackoverflow.com/a/33773390/5031275). The other alternative is to run each query seperately and merge the results. But most likely you should re-evaluate exactly what you think this will do for you. – Blakes Seven Mar 15 '16 at 00:47
  • Your detailed explanation helped me to understand these concepts clearly. Thank you very much for taking your time to answer.. – Yasitha Waduge Mar 15 '16 at 02:36

1 Answers1

1

The $geoWithin operator does not return sorted results. As a result MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results. So in short use $near or $nearSphere so that you can get sorted results. see docs:

The $geoWithin operator does not return sorted results. As such, MongoDB can return $geoWithin queries more quickly than geospatial $near or $nearSphere queries, which sort results.

Yan Foto
  • 10,850
  • 6
  • 57
  • 88
Tamil Arasi
  • 179
  • 5
  • Agree with you, however I should be able to use full text search functionality with this, therefore I can't use $near or $nearSphere with $text. Doc says: You cannot combine the $near operator, which requires a special geospatial index, with a query operator or command that requires another special index. For example you cannot combine $near with the $text query. – Yasitha Waduge Mar 14 '16 at 09:54
  • db.places.aggregate([ { $geoNear: { near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] }, distanceField: "dist.calculated", maxDistance: 2, query: { type: "public" }, includeLocs: "dist.location", num: 5, spherical: true } } ]) – Tamil Arasi Mar 14 '16 at 11:47
  • Try Using this It looks like the aggregation framework has to be used, not find, in order to include the distance: Please Refer this http://docs.mongodb.org/manual/reference/operator/aggregation/geoNear/#pipe._S_geoNear – Tamil Arasi Mar 14 '16 at 11:49
  • 3
    This is just a direct citation from the manual page [$geoWithin - Unsorted Results](https://docs.mongodb.org/manual/reference/operator/query/geoWithin/#unsorted-results) without actually noting it is a citation. This is not an aswer. – Blakes Seven Mar 15 '16 at 00:43