15

It looks like mongodb offers two similar functions for geospatial queries - $near and $geoNear. According to the mongo docs

The geoNear command provides an alternative to the $near operator. In addition to the functionality of $near, geoNear returns additional diagnostic information.

It looks like geoNear provides a superset of the near functionality. For example, near seems to only return the closest 100 documents, whereas geoNear lets you specify a maximum. Is there a reason to use near instead of geoNear? Is one more efficient than the other?

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

4 Answers4

20

Efficiency should be identical for either.

geoNear's major limitation is that as a command it can return a result set up to the maximum document size as all of the matched documents are returned in a single result document. It also requires that a distance field be added to each result document which may or may not be an issue depending on your usage.

$near is a query operator so the results can be larger than a single document (they are still returned in a single response but not a single document). You can also set the maximum number of documents via the query's limit().

I tend to recommend that users stick with the $near unless they need the diagnostics (e.g., distance, or location matched) from the geonear command.

Rob Moore
  • 3,343
  • 17
  • 18
  • Thanks for the clarification. One thing that is unclear is that the docs for near say that it returns 100 documents, but in my testing with mongo 2.4, that doesn't seem to be the case. It seems to return all documents unless limit is used. Do you know what the behavior should be? – Jeff Storey Mar 21 '13 at 03:39
  • Short answer: No I don't know what the expected 2.4 behavior is. According to this Jira ticket (https://jira.mongodb.org/browse/SERVER-5236) they still have not fixed the "progressive" results from the $near queries so it will stop at some point. What you may be seeing is the query just returning as many results as possible in the first batch but eventually the size of that batch should be exhausted. Rob. – Rob Moore Mar 21 '13 at 03:54
  • at least it seems to not be limited to 100, which I thought was the case. thanks for the help. – Jeff Storey Mar 21 '13 at 04:03
  • I think that the size limitation is not a problem since 2.6, because now the aggregate command might return a cursor, not a document (and it's by default). – Paweł Poręba Apr 29 '16 at 10:33
  • UPDATE 2020: "You cannot use $near or $nearSphere in $match queries as part of the aggregation pipeline." Source: https://docs.mongodb.com/manual/reference/operator/aggregation/match/ – pa1nd May 16 '20 at 00:06
3

These are major differences :-

  1. $geoNear also gives you distance from the point but $near command doesn't.

  2. $geoNear command requires that the collection have at most only one 2d index and/or only one 2dsphere index whereas geospatial query operators like $near and $geoWithin permit collections to have multiple geospatial indexes. This is because in $geoNear command there is no option to specify the field on which you want to search, where as in $near command you can specify the field name.

Manish
  • 61
  • 1
  • 1
  • 5
3

The main difference is that $near is a query operator, but $geoNear is an aggregation stage. Both return documents in order of nearest to farthest from the given point.

What it means is that $near can be used in find() queries or in the $match aggregation stage, but $geoNear cannot. Instead $geoNear must be used as a separate aggregation stage only.

The options each feature provides also differ. I invite you to review the details in the corresponding documentation sections:

$near documentation
$geoNear documentation

Jan Švábík
  • 314
  • 2
  • 13
HbnKing
  • 1,762
  • 1
  • 11
  • 25
1

The 100 documents limit with GeoNear is the default behaviour but you can just set the num fields as described on the mongodb documentation (http://docs.mongodb.org/manual/reference/command/geoNear/)

Default is set to 100 but you can set more. Unfortunately skip parameter is missing for the moment (see https://jira.mongodb.org/browse/SERVER-3925)

Kim D.
  • 806
  • 10
  • 15
  • I thought near also defaults 100 but that doesn't seem to be the case – Jeff Storey Mar 21 '13 at 04:24
  • FWIW, here is why there is some confusion over the limit of 100 documents... There used to be a limit. Now the limit is removed: "Starting in version 4.2, MongoDB removes the limit and num options for the $geoNear stage as well as the default limit of 100 documents. " from here: https://docs.mongodb.com/manual/release-notes/4.2-compatibility/#compat-geonear – Jon Kern Jan 04 '20 at 15:48