Chapter 2: Basic Aggregation - Utility Stages | Lecture: $geoNear Stage


I suspect if the course material is partly outdated or simply wrong given that the lecture 2 about $geoNear stage is not compatible with MongoDB documentation

Here’s a screenshot from lecture describing the $geoNear options:

  1. First Issue:

[00:48 - 00:53] One last thing, $geoNear requires that the collection we’re performing our aggregations on to have one and only one geoindex.

I don’t think the above statement is true, given the fact that key field can be specified in for $geoNear options, which specifies the geospatial indexed field to use when calculating the distance.

  1. Second Issue:

[01:07] Required arguments are near, distanceField, and spherical.

The $geoNear definition in the documentation clearly shows and states that the only required fields are near and distanceField, and not spherical field.

  1. Third Issue:
    Both limit and num fields are removed as stated in the docs, but are used in the lecture.

Could you clarify the above states issues?

Hi @Omer_Toraman1, welcome to the community.
A lot has been changed in the way GeoSpatial queries work in MongoDB since the lecture got recorded, as a matter of fact, we are working on revamping the course and it is going to get published in the near future.

But to clarify on the issues that you have raised:

Yes, starting in MongoDB 4.0, you can specify a key option to the $geoNear pipeline stage to indicate the indexed field path to use. This allows the $geoNear stage to be used on a collection that has multiple 2dsphere index and/or multiple 2d index:

  • If your collection has multiple 2dsphere index and/or multiple 2d index, you must use the key option to specify the indexed field path to use.
  • If you do not specify the key , you cannot have multiple 2dsphere index and/or multiple 2d index since without the key , index selection among multiple 2d indexes or 2dsphere indexes is ambiguous.

Correct, but in 3.6 and before, it is required if using a 2dsphere index. Determines how MongoDB calculates the distance. The default value is false.

Yes, 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. To limit the results of $geoNear , use the $geoNear stage with the $limit stage.

I hope it helps, if you want to be a part of our beta testing program for M121 v2 please let us know.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer