Skip to content

Commit f195a54

Browse files
schmallisokay-kim
authored andcommitted
DOCS-11099: clarifies spherical behavior, adds keys param to geoNear
1 parent 0364ae7 commit f195a54

File tree

8 files changed

+265
-62
lines changed

8 files changed

+265
-62
lines changed

source/includes/apiargs-dbcommand-geoNear-field.yaml

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ type:
2828
---
2929
arg_name: field
3030
description: |
31-
Required if using a ``2dsphere`` index. Determines how MongoDB
32-
calculates the distance. The default value is ``false``.
31+
Determines how MongoDB calculates the distance between two points:
32+
33+
- When ``true``, MongoDB uses :query:`$nearSphere` semantics and
34+
calculates distances using spherical geometry.
35+
36+
- When ``false``, MongoDB uses :query:`$near` semantics:
37+
spherical geometry for :doc:`2dsphere </core/2dsphere>`
38+
indexes and planar geometry for :doc:`2d </core/2d>` indexes.
3339
34-
.. include:: /includes/extracts/geoNear-distance-calculation-spherical-field.rst
40+
*Default: false.*
3541
36-
If ``false``, then MongoDB uses 2d planar geometry to calculate
37-
distance between points.
38-
39-
If using a :doc:`2dsphere </core/2dsphere>` index, ``spherical`` must
40-
be ``true``.
4142
interface: command
4243
name: spherical
4344
operation: geoNear
@@ -74,8 +75,6 @@ description: |
7475
be. MongoDB filters the results to those documents that are *at
7576
least* the specified distance from the center point.
7677
77-
Only available for use with :doc:`2dsphere </core/2dsphere>` index.
78-
7978
Specify the distance in meters if the specified point is
8079
:term:`GeoJSON` and in radians if the specified point is
8180
:term:`legacy coordinate pairs <legacy coordinate pairs>`.
@@ -173,4 +172,28 @@ optional: true
173172
operation: geoNear
174173
arg_name: field
175174
interface: dbcommand
175+
---
176+
arg_name: field
177+
name: key
178+
type: string
179+
operation: geoNear
180+
interface: dbcommand
181+
position: 13
182+
description: |
183+
Specify the geospatial index to use when calculating the distance.
184+
If your collection has multiple geospatial indexes, you **must**
185+
use the ``key`` option to specify the indexed field path to use.
186+
{{example}} provides a full example.
187+
188+
If you do not specify the field over which to perform the search with
189+
``key``, MongoDB will attempt to look for a usable ``2d`` or
190+
``2dsphere`` index, in that order. If there is more than one ``2d``
191+
index or more than one ``2dsphere`` index, MongoDB will return an
192+
error.
193+
194+
.. versionadded:: 4.0
195+
196+
replacement:
197+
example: ":ref:`dbcommand-geoNear-key-param-example`"
198+
optional: true
176199
...

source/includes/apiargs-pipeline-geoNear-field.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ source:
8686
file: apiargs-dbcommand-geoNear-field.yaml
8787
ref: uniqueDocs
8888
---
89-
position: 10
89+
position: 11
9090
name: minDistance
9191
type: number
9292
optional: true
@@ -103,4 +103,14 @@ description: |
103103
interface: pipeline
104104
operation: geoNear
105105
arg_name: field
106+
---
107+
arg_name: field
108+
interface: pipeline
109+
operation: geoNear
110+
source:
111+
file: apiargs-dbcommand-geoNear-field.yaml
112+
ref: key
113+
replacement:
114+
example: ":ref:`pipeline-geoNear-key-param-example`"
115+
position: 12
106116
...

source/reference/command/geoNear.txt

Lines changed: 111 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ Considerations
4141
--------------
4242

4343
.. include:: /includes/extracts/geoNear-command-index-requirement.rst
44-
45-
However, the :dbcommand:`geoNear` command requires that a collection
46-
have *at most* only one :doc:`2dsphere </core/2dsphere>` and/or only
47-
one :doc:`2d index </core/2d>` .
44+
If you have more than one geospatial index on the collection, use the
45+
``keys`` parameter to specify which field to use in the calculation. If
46+
you have only one geospatial index, :dbcommand:`geoNear` implicitly uses
47+
the indexed field for the calculation.
4848

4949
.. include:: /includes/extracts/views-unsupported-geoNear.rst
5050

@@ -60,29 +60,28 @@ Command Syntax
6060
If using a ``2dsphere`` index, you can specify either a ``GeoJSON``
6161
point or a legacy coordinate pair for the ``near`` value.
6262

63-
You must include ``spherical: true`` in the syntax.
64-
65-
With ``spherical: true``, if you specify a GeoJSON point, MongoDB uses
63+
If you specify a GeoJSON point, MongoDB uses
6664
meters as the unit of measurement:
6765

6866
.. code-block:: javascript
6967

7068
db.runCommand( {
71-
geoNear: <collection> ,
72-
near: { type: "Point" , coordinates: [ <coordinates> ] } ,
73-
spherical: true,
69+
geoNear : <collection> ,
70+
near : { type : "Point" , coordinates : [ <coordinates> ] } ,
71+
spherical : true,
7472
...
7573
} )
7674

77-
With ``spherical: true``, if you specify a legacy coordinate pair,
75+
If you specify a legacy coordinate pair, you *must* specify ``spherical : true``
76+
With ``spherical : true`` and a legacy coordinate pair,
7877
MongoDB uses radians as the unit of measurement:
7978

8079
.. code-block:: javascript
8180

8281
db.runCommand( {
83-
geoNear: <collection> ,
84-
near: [ <coordinates> ],
85-
spherical: true,
82+
geoNear : <collection> ,
83+
near : [ <coordinates> ],
84+
spherical : true,
8685
...
8786
} )
8887

@@ -94,12 +93,12 @@ To query a :doc:`2d </core/2d>` index, use the following syntax:
9493
.. code-block:: javascript
9594

9695
db.runCommand( {
97-
geoNear: <collection>,
96+
geoNear : <collection>,
9897
near : [ <coordinates> ],
9998
...
10099
} )
101100

102-
If you specify ``spherical: true``, MongoDB uses spherical geometry to
101+
If you specify ``spherical : true``, MongoDB uses spherical geometry to
103102
calculate distances in radians. Otherwise, MongoDB uses planar geometry
104103
to calculate distances between points.
105104

@@ -115,16 +114,15 @@ farthest, the ``minDistance`` field effectively skips over the first
115114
*n* documents where *n* is determined by the distance requirement.
116115

117116
The :dbcommand:`geoNear` command provides an alternative to the
118-
:query:`$near` operator. In addition to the functionality of
119-
:query:`$near`, :dbcommand:`geoNear` returns additional diagnostic
117+
:query:`$near` and :query:`$nearSphere` operators.
118+
In addition to the functionality of
119+
:query:`$near` and :query:`$nearSphere`, :dbcommand:`geoNear` returns diagnostic
120120
information.
121121

122122
In a :term:`sharded cluster`, the :dbcommand:`geoNear` command may return
123123
:term:`orphaned documents <orphaned document>`. To avoid this, consider
124124
using the :pipeline:`$geoNear` aggregation stage as an alternative.
125125

126-
.. read-lock, slave-ok
127-
128126
Examples
129127
--------
130128

@@ -240,8 +238,100 @@ The operation returns the following output:
240238
"ok" : 1
241239
}
242240

241+
.. _dbcommand-geoNear-key-param-example:
242+
243+
Specify Which Geospatial Index to Use
244+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245+
246+
.. versionadded:: 4.0
247+
248+
Consider a ``places`` collection that has a :doc:`2dsphere
249+
</core/2dsphere>` index on the ``location`` field **and** a
250+
:doc:`2d </core/2d>` index on the ``legacy`` field.
251+
252+
A document in the ``places`` collection resembles the following:
253+
254+
.. code-block:: javascript
255+
256+
{
257+
"_id" : 3,
258+
"name" : "Polo Grounds",
259+
"location": {
260+
"type" : "Point",
261+
"coordinates" : [ -73.9375, 40.8303 ]
262+
},
263+
"legacy" : [ -73.9375, 40.8303 ],
264+
"category" : "Stadiums"
265+
}
266+
267+
The following example uses the ``keys`` parameter to specify that the
268+
operation should use the ``location`` field values for the
269+
:pipeline:`$geoNear` operation rather than the ``legacy`` field values.
270+
271+
.. code-block:: javascript
272+
273+
db.runCommand(
274+
{
275+
geoNear : "places",
276+
near : { type : "Point", coordinates : [ -73.98142 , 40.71782 ] },
277+
key : "location",
278+
query : { "category" : "Parks" }
279+
}
280+
)
281+
282+
The operation returns the following:
283+
284+
.. code-block:: javascript
285+
286+
{
287+
"results" : [
288+
{
289+
"dis" : 974.175764916902,
290+
"obj" : {
291+
"_id" : 2,
292+
"name" : "Sara D. Roosevelt Park",
293+
"location" : {
294+
"type" : "Point",
295+
"coordinates" : [
296+
-73.9928,
297+
40.7193
298+
]
299+
},
300+
"category" : "Parks"
301+
}
302+
},
303+
{
304+
"dis" : 5887.92792958097,
305+
"obj" : {
306+
"_id" : 1,
307+
"name" : "Central Park",
308+
"location" : {
309+
"type" : "Point",
310+
"coordinates" : [
311+
-73.97,
312+
40.77
313+
]
314+
},
315+
"legacy" : [
316+
-73.97,
317+
40.77
318+
],
319+
"category" : "Parks"
320+
}
321+
}
322+
],
323+
"stats" : {
324+
"nscanned" : 19,
325+
"objectsLoaded" : 6,
326+
"avgDistance" : 3431.051847248936,
327+
"maxDistance" : 5887.92792958097,
328+
"time" : 2946
329+
},
330+
"ok" : 1
331+
332+
243333
Override Default Read Concern
244-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
334+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245335

246336
To override the default read concern level of :readconcern:`"local"`,
247337
use the ``readConcern`` option.

source/reference/operator/aggregation/geoNear.txt

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,10 @@ When using :pipeline:`$geoNear`, consider that:
4444
the calculated distance.
4545

4646
- .. include:: /includes/extracts/geoNear-stage-index-requirement.rst
47-
48-
- The :pipeline:`$geoNear` requires that a collection have *at most*
49-
only one :doc:`2d index </core/2d>` and/or only one
50-
:doc:`2dsphere index </core/2dsphere>`.
51-
52-
- You do not need to specify which field in
53-
the documents hold the coordinate pair or point. Because
54-
:pipeline:`$geoNear` requires that the collection have a single
55-
geospatial index, :pipeline:`$geoNear` implicitly uses the indexed
56-
field.
57-
58-
- If using a :doc:`2dsphere index </core/2dsphere>`, you must specify
59-
``spherical: true``.
47+
If you have more than one geospatial index on the collection, use the
48+
``keys`` parameter to specify which field to use in the calculation.
49+
If you have only one geospatial index, :pipeline:`$geoNear` implicitly
50+
uses the indexed field for the calculation.
6051

6152
.. |geoNear| replace:: :pipeline:`$geoNear` stage
6253

@@ -152,3 +143,86 @@ specified distance from the center point.
152143
}
153144
}
154145
])
146+
147+
.. _pipeline-geoNear-key-param-example:
148+
149+
Specify Which Geospatial Index to Use
150+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151+
152+
.. versionadded:: 4.0
153+
154+
Consider a ``places`` collection that has a :doc:`2dsphere
155+
</core/2dsphere>` index on the ``location`` field and a
156+
:doc:`2d </core/2d>` index on the ``legacy`` field.
157+
158+
A document in the ``places`` collection resembles the following:
159+
160+
.. code-block:: javascript
161+
162+
{
163+
"_id" : 3,
164+
"name" : "Polo Grounds",
165+
"location": {
166+
"type" : "Point",
167+
"coordinates" : [ -73.9375, 40.8303 ]
168+
},
169+
"legacy" : [ -73.9375, 40.8303 ],
170+
"category" : "Stadiums"
171+
}
172+
173+
The following example uses the ``keys`` option to specify that the
174+
aggregation should use the ``location`` field values for the
175+
:pipeline:`$geoNear` operation rather than the ``legacy`` field values.
176+
177+
.. code-block:: javascript
178+
179+
db.places.aggregate([
180+
{
181+
$geoNear: {
182+
near: { type: "Point", coordinates: [ -73.98142 , 40.71782 ] },
183+
key: "location",
184+
distanceField: "dist.calculated",
185+
query: { "category": "Parks" }
186+
}
187+
}
188+
])
189+
190+
The aggregation returns the following:
191+
192+
.. code-block:: javascript
193+
194+
{
195+
"_id" : 2,
196+
"name" : "Sara D. Roosevelt Park",
197+
"location" : {
198+
"type" : "Point",
199+
"coordinates" : [
200+
-73.9928,
201+
40.7193
202+
]
203+
},
204+
"category" : "Parks",
205+
"dist" : {
206+
"calculated" : 974.175764916902
207+
}
208+
}
209+
{
210+
"_id" : 1,
211+
"name" : "Central Park",
212+
"location" : {
213+
"type" : "Point",
214+
"coordinates" : [
215+
-73.97,
216+
40.77
217+
]
218+
},
219+
"legacy" : [
220+
-73.97,
221+
40.77
222+
],
223+
"category" : "Parks",
224+
"dist" : {
225+
"calculated" : 5887.92792958097
226+
}
227+
}
228+

source/reference/operator/query/minDistance.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ Definition
2121
:query:`$nearSphere` query to those documents that are *at least* the
2222
specified distance from the center point.
2323

24-
:query:`$minDistance` is available for use with :doc:`2dsphere
25-
</core/2dsphere>` index only.
26-
2724
If :query:`$near` or :query:`$nearSphere` query specifies the center
2825
point as a :ref:`GeoJSON point <geojson-point>`, specify the distance
2926
as a non-negative number in *meters*.

0 commit comments

Comments
 (0)