@@ -204,8 +204,147 @@ When using :pipeline:`$geoNear`, consider that:
204
204
- Starting in version 4.2, :pipeline:`$geoNear` no longer has a default
205
205
limit of 100 documents.
206
206
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
+ ~~~~~~~~~~~~~~~~
209
348
210
349
.. note::
211
350
0 commit comments