Skip to content

Commit 82caaae

Browse files
committed
DOCS-14779 Add context around 5.1 let usage in geoNear
1 parent 2405b95 commit 82caaae

File tree

3 files changed

+147
-2
lines changed

3 files changed

+147
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Starting in MongoDB 5.1, the ``near`` parameter in the :pipeline:`$geoNear`
2+
aggregation stage supports the :ref:`let option <geoNear_let_example>` and
3+
:ref:`bound let option <geoNear_bounded_let_example>`.

source/reference/operator/aggregation/geoNear.txt

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,15 @@ When using :pipeline:`$geoNear`, consider that:
204204
- Starting in version 4.2, :pipeline:`$geoNear` no longer has a default
205205
limit of 100 documents.
206206

207-
Example
208-
-------
207+
- Starting in MongoDB 5.1, the ``near`` parameter supports the
208+
:ref:`let option <geoNear_let_example>` and
209+
:ref:`bound let option <geoNear_bounded_let_example>`.
210+
211+
Examples
212+
--------
213+
214+
Maximum Distance
215+
~~~~~~~~~~~~~~~~
209216

210217
.. note::
211218

@@ -287,6 +294,136 @@ equal to ``Parks``.
287294
}
288295
])
289296

297+
.. _geoNear_let_example:
298+
299+
$geoNear with the ``let`` option
300+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
301+
302+
In this example:
303+
304+
- The ``let`` option is used to set an array value of
305+
``[-73.99279,40.719296]`` to the variable ``$pt``.
306+
307+
- ``$pt`` is specified as a let option to the ``near`` parameter in the
308+
``$geoNear`` stage.
309+
310+
.. code-block:: javascript
311+
:emphasize-lines: 6,16
312+
313+
db.places.aggregate(
314+
[
315+
{
316+
"$geoNear":
317+
{
318+
"near":"$$pt",
319+
"distanceField":"distance",
320+
"maxDistance":2,
321+
"query":{"category":"Parks"},
322+
"includeLocs":"dist.location",
323+
"spherical":true
324+
}
325+
}
326+
],
327+
{
328+
"let":{ "pt": [ -73.99279, 40.719296 ] }
329+
}
330+
)
331+
332+
The aggregation returns the following:
333+
334+
.. code-block:: javascript
335+
:copyable: false
336+
337+
{
338+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
339+
name: 'Sara D. Roosevelt Park',
340+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
341+
category: 'Parks',
342+
distance: 1.4957325341976439e-7,
343+
dist: { location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] } }
344+
},
345+
{
346+
_id: ObjectId("61715cf9b0c1d171bb498fd6"),
347+
name: 'Central Park',
348+
location: { type: 'Point', coordinates: [ -73.97, 40.77 ] },
349+
category: 'Parks',
350+
distance: 0.0009348548688841822,
351+
dist: { location: { type: 'Point', coordinates: [ -73.97, 40.77 ] } }
352+
}
353+
354+
.. _geoNear_bounded_let_example:
355+
356+
$geoNear with Bound ``let`` Option
357+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
358+
359+
The ``let`` option can bind a variable which can be used in a
360+
$geoNear query.
361+
362+
In this example, :pipeline:`$lookup` uses:
363+
364+
- ``let`` to define ``$pt``.
365+
- :pipeline:`$geoNear` in the ``pipeline``.
366+
- ``$pt`` to define ``near`` in the :pipeline:`$geoNear` pipeline stage.
367+
368+
.. code-block:: javascript
369+
:emphasize-lines: 5
370+
371+
db.places.aggregate( [
372+
{
373+
$lookup:{
374+
from: "places",
375+
let: { pt: "$location" },
376+
pipeline: [
377+
{
378+
$geoNear: {
379+
near: "$$pt",
380+
distanceField: "distance"
381+
}
382+
}
383+
],
384+
as: "joinedField"
385+
}
386+
},
387+
{
388+
$match: { name: "Sara D. Roosevelt Park" }
389+
}
390+
] );
391+
392+
The aggregation returns the following:
393+
394+
.. code-block:: javascript
395+
:copyable: false
396+
397+
{
398+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
399+
name: 'Sara D. Roosevelt Park',
400+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
401+
category: 'Parks',
402+
joinedField: [
403+
{
404+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
405+
name: 'Sara D. Roosevelt Park',
406+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
407+
category: 'Parks',
408+
distance: 0
409+
},
410+
{
411+
_id: ObjectId("61715cf9b0c1d171bb498fd6"),
412+
name: 'Central Park',
413+
location: { type: 'Point', coordinates: [ -73.97, 40.77 ] },
414+
category: 'Parks',
415+
distance: 5962.448255234964
416+
},
417+
{
418+
_id: ObjectId("61715cfab0c1d171bb498fd8"),
419+
name: 'Polo Grounds',
420+
location: { type: 'Point', coordinates: [ -73.9375, 40.8303 ] },
421+
category: 'Stadiums',
422+
distance: 13206.535424939102
423+
}
424+
]
425+
}
426+
290427
.. _pipeline-geoNear-key-param-example:
291428

292429
Specify Which Geospatial Index to Use

source/release-notes/5.1.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Aggregation
2020

2121
.. _5.1-rel-notes-new-agg-operators:
2222

23+
$geoNear accepts ``let-bound`` variables
24+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25+
26+
.. include:: /includes/fact-5.1-geonear-let-allowed.rst
27+
2328
$lookup and $graphLookup with sharded collections
2429
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2530

0 commit comments

Comments
 (0)