-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Geo operators #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Geo operators #99
Changes from all commits
c0aa991
106296c
d1d3c4e
cf97299
3b0e8ad
7386d1c
b58c916
2a97496
20560e5
a6181f0
7d4e03f
80661ec
d35e3b6
6c54340
6fab9e2
768e70a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,62 +271,116 @@ Geospatial | |
db.collection.find( { location: { $within: { shape } } } ); | ||
|
||
Replace ``{ shape }`` with a document that describes a shape. The | ||
:operator:`$within` command supports three shapes. These shapes and the | ||
relevant expressions follow: | ||
:operator:`$within` operator supports three basic shape types. These | ||
shapes are: | ||
|
||
- Rectangles. Use the :operator:`$box` shape, consider the following | ||
variable and :operator:`$within` document: | ||
.. operator:: $box | ||
|
||
.. code-block:: javascript | ||
The :operator:`$box` operator specifies a rectangular shape for | ||
the :operator:`$within` operator. To use the :operator:`$box` | ||
operator, you must specify the bottom left and top right corners | ||
of the rectangle in an array object. Consider the following | ||
example: | ||
|
||
db.collection.find( { location: { $within: { $box: [[100,0], [120,100]] } } } ); | ||
.. code-block:: javascript | ||
|
||
db.collection.find( { loc: { $within: { $box: [ [0,0], [100,100] ] } } } ) | ||
|
||
This will return all the documents that are within the box | ||
having points: ``[0,0]``, ``[0,100]``, ``[100,0]``, and ``[100,100]``. | ||
|
||
.. operator:: $center | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it circle or center? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wiki says: center I think the docs themselves need to be updated to reflect this point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, command is $center btw, I don't see this line in the current version... is this a comment on an earlier commit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That says circle. I'm not sure if the other geo documents make this mistake, but we should check... |
||
|
||
This specifies a circle shape for the :operator:`$within` | ||
operators. To define the bounds of a query using | ||
:operator:`$center`, you must specify: | ||
|
||
- the center point, and | ||
|
||
- the radius | ||
|
||
Here a box, ``[[100,120], [100,0]]`` describes the parameter | ||
for the query. As a minimum, you must specify the lower-left and | ||
upper-right corners of the box. | ||
Considering the following example: | ||
|
||
- Circles. Specify circles in the following form: | ||
.. code-block:: javascript | ||
|
||
db.collection.find( { location: { $within: { $center: [ [0,0], 10 } } } ); | ||
|
||
.. code-block:: javascript | ||
The above command returns all the documents that fall within a | ||
10 unit radius of the point ``[0,0]``. | ||
|
||
db.collection.find( { location: { $within: { $circle: [ center, radius } } } ); | ||
.. operator:: $polygon | ||
|
||
- Polygons. Specify polygons with an array of points. See the | ||
following example: | ||
Use :operator:`$polygon` to specify a polgon for a bounded query | ||
using the :operator:`$within` operator. To define the polygon, | ||
you must specify an array of coordinate points, as in the | ||
following: | ||
|
||
.. code-block:: javascript | ||
[ [ x1,y1 ], [x2,y2], [x3,y3] ] | ||
|
||
db.collection.find( { location: { $within: { $box: [[100,120], [100,100], [120,100], [240,200]] } } } ); | ||
The last point specified is always implicitly connected to the | ||
first. You can specify as many points, and therfore sides, as | ||
you like. Consider the following bounded query for documents | ||
with coordinates within a polygon: | ||
|
||
.. code-block:: javascript | ||
|
||
The last point of a polygon is implicitly connected to the first | ||
point. | ||
db.collection.find( { loc: { $within: { $polygon: [ [0,0], [3,6], [6,0] ] } } } ) | ||
|
||
All shapes include the border of the shape as part of the shape, | ||
although this is subject to the imprecision of floating point | ||
numbers. | ||
calculations. | ||
|
||
.. operator:: $nearSphere | ||
|
||
The :operator:`$nearSphere` operator is the spherical equivalent of | ||
the :operator:`$near` operator. :operator:`$nearSphere` returns all | ||
documents near a point, calculating distances using spherical geometry. | ||
|
||
.. code-block:: javascript | ||
|
||
db.collection.find( { loc: { $nearSphere: [0,0] } } ) | ||
|
||
.. operator:: $centerSphere | ||
|
||
The :operator:`$centerSphere` operator is the spherical equivalent | ||
of the :operator:`$center` operator. :operator:`$centerSphere` uses | ||
spherical geometry to calculate distances in a circle specified by | ||
a point and radius. | ||
|
||
Considering the following example: | ||
|
||
.. code-block:: javascript | ||
|
||
db.collection.find( { loc: { $centerSphere: { [0,0], 10 / 3959 } } } ) | ||
|
||
This query will return all documents within a 10 mile radius of | ||
``[0,0]`` using a spherical geometry to calculate distances. | ||
|
||
.. operator:: $uniqueDocs | ||
|
||
When using the :dbcommand:`geoNear`, if document contains more than | ||
one field with coordinate values, MongoDB will return the same | ||
document multiple times. When using the :operator:`$within`, | ||
however, MongoDB provides opposite behavior: if a document contains | ||
more than one field with coordinate values, MongoDB will only | ||
return the document once. | ||
.. TODO read aloud | ||
|
||
The :operator:`$uniqueDocs` operator overrides these default | ||
behaviors. | ||
The :operator:`$uniqueDocs` operator overrides default query | ||
behavior when :ref:`multiple location documents | ||
<geospatial-query-multi-location>` are used. | ||
|
||
By specifying ``$uniqueDocs: false`` in a :operator:`$within` | ||
query, will cause the query to return a single document multiple | ||
times if there is more than one match. | ||
When querying multiple location documents using the | ||
:operator:`$within` operator, MongoDB will return matching documents | ||
once, even if there are multiple matches in the location fields. | ||
|
||
By contrast, if you specify ``uniqueDocs: true`` as an option to | ||
the a :dbcommand:`geoNear` command, then :dbcommand:`geoNear` only | ||
returns a single document even if there are multiple matches. | ||
By specifying ``$uniqueDocs: false`` in the :operator:`$within` | ||
operator, matching documents may be returned multiple times if | ||
there are multiple matches in the location field. | ||
|
||
.. code-block:: javascript | ||
|
||
db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } ) | ||
|
||
.. note:: | ||
|
||
You cannot specify :operator:`$uniqueDocs` with :operator:`$near` | ||
or haystack queries. | ||
You cannot specify :operator:`$uniqueDocs` with | ||
:operator:`$near` or :dbcommand:`geoSearch` for :ref:`haystack | ||
indexes <geospatial-haystack-index>`. | ||
|
||
.. index:: query selectors; logical | ||
.. _query-selectors-logical: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks incomplete.