Mongodb geoNear vs near

Mongodb seems to offer two similar features for geospatial queries - $near and $geoNear . According to mongo docs

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

It seems that geoNear provides a superset of near functionality. For example, near seems to return only the next 100 documents, while geoNear allows geoNear to specify a maximum. Is there any reason to use near instead of geoNear ? Is more effective than the other?

+12
mongodb geospatial
source share
4 answers

Efficiency should be the same for both.

geoNear The main limitation is that, as a command, it can return the result with the maximum document size, since all agreed documents are returned in a single resulting document. It also requires that a distance field be added to each resulting document, which may or may not be a problem depending on your use.

$near is a query operator, so the results can be more than one document (they are still returned in one answer, but not in one document). You can also set the maximum number of documents using the query limit ().

I recommend users stick with $near unless they require diagnostics (such as distance or location) from the geoNear team.

+16
source share

A limit of 100 documents using GeoNear is the default behavior, but you can simply set the num fields as described in the mongodb documentation ( http://docs.mongodb.org/manual/reference/command/geoNear/ )

The default value is 100, but you can set more. Unfortunately, the skip option is missing at the moment (see https://jira.mongodb.org/browse/SERVER-3925 )

+1
source share

These are the main differences: -

  • $ geoNear also gives you the distance from the point, but $ near command does not.

    Team
  • $ geoNear requires a collection to have at most one 2d index and / or only one 2dsphere index, while geospatial query operators such as $ near and $ geoWithin allow collections to have multiple geospatial indexes. This is due to the fact that in the $ geoNear command it is not possible to specify the field in which you want to search, where, as in $ near command, you can specify the name of the field.

+1
source share

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

This means that $ near can be used in find () queries or at the $match aggregation stage, but $geoNear cannot. Instead, $geoNear should only be used as a separate aggregation step.

The options provided by each function also vary. I suggest you familiarize yourself with the details in the relevant sections of the documentation:

$ near documentation
$ GeoNear documentation

0
source share

All Articles