Skip to content

Commit ca66a80

Browse files
committed
DOCS-14779 Add context around 5.1 let usage in geoNear
1 parent 2d8d7b8 commit ca66a80

File tree

3 files changed

+150
-3
lines changed

3 files changed

+150
-3
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: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,147 @@ 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+
.. _geoNear_let_example:
215+
216+
$geoNear with the ``let`` option
217+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
218+
219+
In this example:
220+
221+
- The ``let`` option is used to set an array value of
222+
``[-73.99279,40.719296]`` to the variable ``$pt``.
223+
224+
- ``$pt`` is a let option to the ``near`` parameter to the ``$geoNear``
225+
stage.
226+
227+
.. code-block:: javascript
228+
:emphasize-lines: 6,16
229+
230+
db.places.aggregate(
231+
[
232+
{
233+
"$geoNear":
234+
{
235+
"near":"$$pt",
236+
"distanceField":"distance",
237+
"maxDistance":2,
238+
"query":{"category":"Parks"},
239+
"includeLocs":"dist.location",
240+
"spherical":true
241+
}
242+
}
243+
],
244+
{
245+
"let":{ "pt": [ -73.99279, 40.719296 ] }
246+
}
247+
)
248+
249+
The aggregation returns the following:
250+
251+
.. code-block:: javascript
252+
:copyable: false
253+
254+
{
255+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
256+
name: 'Sara D. Roosevelt Park',
257+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
258+
category: 'Parks',
259+
distance: 1.4957325341976439e-7,
260+
dist: { location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] } }
261+
},
262+
{
263+
_id: ObjectId("61715cf9b0c1d171bb498fd6"),
264+
name: 'Central Park',
265+
location: { type: 'Point', coordinates: [ -73.97, 40.77 ] },
266+
category: 'Parks',
267+
distance: 0.0009348548688841822,
268+
dist: { location: { type: 'Point', coordinates: [ -73.97, 40.77 ] } }
269+
}
270+
271+
.. _geoNear_bounded_let_example:
272+
273+
$geoNear With Bound ``let`` Option
274+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
275+
276+
The ``let`` option can bind a variable which can be used in a
277+
$geoNear query.
278+
279+
In this example, :pipeline:`$lookup` uses:
280+
281+
- ``let`` to define ``$pt``.
282+
- :pipeline:`$geoNear` in the ``pipeline``.
283+
- ``$pt`` to define ``near`` in the :pipeline:`$geoNear` pipeline stage.
284+
285+
.. code-block:: javascript
286+
:emphasize-lines: 7
287+
288+
db.places.aggregate(
289+
[
290+
{
291+
$lookup:
292+
{
293+
from: "places",
294+
let: { pt: "$location" },
295+
pipeline: [
296+
{
297+
$geoNear:
298+
{
299+
near: "$$pt",
300+
distanceField: "distance"
301+
}
302+
}
303+
],
304+
as: "joinedField"
305+
}
306+
},
307+
{ $match: { name: "Sara D. Roosevelt Park" } }
308+
]
309+
);
310+
311+
The aggregation returns the following:
312+
313+
.. code-block:: javascript
314+
:copyable: false
315+
316+
{
317+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
318+
name: 'Sara D. Roosevelt Park',
319+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
320+
category: 'Parks',
321+
joinedField: [
322+
{
323+
_id: ObjectId("61715cf9b0c1d171bb498fd7"),
324+
name: 'Sara D. Roosevelt Park',
325+
location: { type: 'Point', coordinates: [ -73.9928, 40.7193 ] },
326+
category: 'Parks',
327+
distance: 0
328+
},
329+
{
330+
_id: ObjectId("61715cf9b0c1d171bb498fd6"),
331+
name: 'Central Park',
332+
location: { type: 'Point', coordinates: [ -73.97, 40.77 ] },
333+
category: 'Parks',
334+
distance: 5962.448255234964
335+
},
336+
{
337+
_id: ObjectId("61715cfab0c1d171bb498fd8"),
338+
name: 'Polo Grounds',
339+
location: { type: 'Point', coordinates: [ -73.9375, 40.8303 ] },
340+
category: 'Stadiums',
341+
distance: 13206.535424939102
342+
}
343+
]
344+
}
345+
346+
Maximum Distance
347+
~~~~~~~~~~~~~~~~
209348

210349
.. note::
211350

source/release-notes/5.1.txt

Lines changed: 6 additions & 1 deletion
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

@@ -138,7 +143,7 @@ the *side* collection (``config.image_collection``).
138143
----------------------------------------------------
139144

140145
Starting in MongoDB 5.1, when starting a :binary:`mongod` with the
141-
:option:`--configsvr <mongod--configsvr>` option you must also
146+
:option:`--configsvr <mongod --configsvr>` option you must also
142147
specify a :option:`--replSet <mongod --replSet>`.
143148

144149
Starting in MongoDB 5.1, when starting a :binary:`mongod` with the

0 commit comments

Comments
 (0)