/
query-a-2d-index.txt
137 lines (93 loc) · 4.19 KB
/
query-a-2d-index.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
====================
Query a ``2d`` Index
====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
The following sections describe queries supported by the ``2d`` index.
Points within a Shape Defined on a Flat Surface
-----------------------------------------------
To select all legacy coordinate pairs found within a given shape on a flat
surface, use the :query:`$geoWithin` operator along with a shape
operator. Use the following syntax:
.. code-block:: javascript
db.<collection>.find( { <location field> :
{ $geoWithin :
{ $box|$polygon|$center : <coordinates>
} } } )
The following queries for documents within a rectangle defined by ``[ 0
, 0 ]`` at the bottom left corner and by ``[ 100 , 100 ]`` at the top
right corner.
.. code-block:: javascript
db.places.find( { loc :
{ $geoWithin :
{ $box : [ [ 0 , 0 ] ,
[ 100 , 100 ] ]
} } } )
The following queries for documents that are within the circle centered
on ``[ -74 , 40.74 ]`` and with a radius of ``10``:
.. code-block:: javascript
db.places.find( { loc: { $geoWithin :
{ $center : [ [-74, 40.74 ] , 10 ]
} } } )
For syntax and examples for each shape, see the following:
- :query:`$box`
- :query:`$polygon`
- :query:`$center` (defines a circle)
Points within a Circle Defined on a Sphere
------------------------------------------
MongoDB supports rudimentary spherical queries on flat ``2d`` indexes for
legacy reasons. In general, spherical calculations should use a ``2dsphere``
index, as described in :doc:`/core/2dsphere`.
To query for legacy coordinate pairs in a "spherical cap" on a sphere,
use :query:`$geoWithin` with the :query:`$centerSphere` operator.
Specify an array that contains:
- The grid coordinates of the circle's center point
- The circle's radius measured in radians. To calculate radians, see
:doc:`/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes`.
Use the following syntax:
.. code-block:: javascript
db.<collection>.find( { <location field> :
{ $geoWithin :
{ $centerSphere : [ [ <x>, <y> ] , <radius> ] }
} } )
The following example query returns all documents within a 10-mile
radius of longitude ``88 W`` and latitude ``30 N``. The example converts
distance to radians by dividing distance by the approximate radius of
the earth, 3959 miles:
.. code-block:: javascript
db.<collection>.find( { loc : { $geoWithin :
{ $centerSphere :
[ [ 88 , 30 ] , 10 / 3959 ]
} } } )
Proximity to a Point on a Flat Surface
--------------------------------------
Proximity queries return the 100 legacy coordinate pairs closest to the
defined point and sort the results by distance. Use either the
:query:`$near` operator or :dbcommand:`geoNear` command. Both require
a ``2d`` index.
The :query:`$near` operator uses the following syntax:
.. code-block:: javascript
db.<collection>.find( { <location field> :
{ $near : [ <x> , <y> ]
} } )
For examples, see :query:`$near`.
The :dbcommand:`geoNear` command uses the following syntax:
.. code-block:: javascript
db.runCommand( { geoNear: <collection>, near: [ <x> , <y> ] } )
The :dbcommand:`geoNear` command offers more options and returns more
information than does the :query:`$near` operator. To run the
command, see :dbcommand:`geoNear`.
.. index:: geospatial queries
.. index:: geospatial queries; exact
.. _geospatial-indexes-exact-match:
Exact Matches on a Flat Surface
-------------------------------
You can use the :method:`db.collection.find()` method to query for an
exact match on a location. These queries use the following syntax:
.. code-block:: javascript
db.<collection>.find( { <location field>: [ <x> , <y> ] } )
This query will return any documents with the value of ``[ <x> , <y> ]``.