diff --git a/source/geospatial-queries.txt b/source/geospatial-queries.txt index c23f6a9c052..21460327c91 100644 --- a/source/geospatial-queries.txt +++ b/source/geospatial-queries.txt @@ -291,36 +291,12 @@ query, used by each geospatial operations: - Flat - -Example -------- +Examples +-------- -Create a collection ``places`` with the following documents: - -.. code-block:: javascript - - db.places.insert( { - name: "Central Park", - location: { type: "Point", coordinates: [ -73.97, 40.77 ] }, - category: "Parks" - } ); - db.places.insert( { - name: "Sara D. Roosevelt Park", - location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] }, - category: "Parks" - } ); - db.places.insert( { - name: "Polo Grounds", - location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] }, - category: "Stadiums" - } ); - -The following operation creates a ``2dsphere`` index on the -``location`` field: - -.. code-block:: javascript - - db.places.createIndex( { location: "2dsphere" } ) +.. include:: /includes/geospatial-places-data-setup.rst +The ``places`` collection above has a ``2dsphere`` index. The following query uses the :query:`$near` operator to return documents that are at least 1000 meters from and at most 5000 meters from the specified GeoJSON point, sorted in order from nearest to diff --git a/source/includes/geospatial-places-data-setup.rst b/source/includes/geospatial-places-data-setup.rst new file mode 100644 index 00000000000..90833abc7fb --- /dev/null +++ b/source/includes/geospatial-places-data-setup.rst @@ -0,0 +1,28 @@ +Create a collection ``places`` with the following documents: + +.. code-block:: javascript + + db.places.insertMany( [ + { + name: "Central Park", + location: { type: "Point", coordinates: [ -73.97, 40.77 ] }, + category: "Parks" + }, + { + name: "Sara D. Roosevelt Park", + location: { type: "Point", coordinates: [ -73.9928, 40.7193 ] }, + category: "Parks" + }, + { + name: "Polo Grounds", + location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] }, + category: "Stadiums" + } + ] ) + +The following operation creates a ``2dsphere`` index on the +``location`` field: + +.. code-block:: javascript + + db.places.createIndex( { location: "2dsphere" } ) \ No newline at end of file diff --git a/source/reference/operator/aggregation/geoNear.txt b/source/reference/operator/aggregation/geoNear.txt index e2e0edbe9a0..71bb2a33dda 100644 --- a/source/reference/operator/aggregation/geoNear.txt +++ b/source/reference/operator/aggregation/geoNear.txt @@ -204,17 +204,22 @@ When using :pipeline:`$geoNear`, consider that: - Starting in version 4.2, :pipeline:`$geoNear` no longer has a default limit of 100 documents. -Example -------- +Examples +-------- + +.. include:: /includes/geospatial-places-data-setup.rst + +Maximum Distance +~~~~~~~~~~~~~~~~ .. note:: .. include:: /includes/extracts/4.2-changes-geoNear-limit.rst -Consider a collection ``places`` that has a ``2dsphere`` index. The -following aggregation uses :pipeline:`$geoNear` to find documents with -a location at most 2 meters from the center ``[ -73.99279 , 40.719296 -]`` and ``category`` equal to ``Parks``. +The ``places`` collection above has a ``2dsphere`` index. +The following aggregation uses :pipeline:`$geoNear` to find documents +with a location at most 2 meters from the center +``[ -73.99279 , 40.719296 ]`` and ``category`` equal to ``Parks``. .. code-block:: javascript