-
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 9 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 |
|---|---|---|
|
|
@@ -948,8 +948,8 @@ Geospatial Commands | |
|
|
||
| Here, :dbcommand:`geoNear` returns the 10 items nearest to the | ||
| coordinates ``[50,50]`` in the collection named | ||
| ``places``. `geoNear` provides the following options (specify all | ||
| distances in the same units as the document coordinate system:) | ||
| ``places``. The :dbcommand:`geoNear` command provides the following options (specify all | ||
| distances in the same units as the document coordinate system.) | ||
|
|
||
| :field near: Takes the coordinates (e.g. ``[ x, y ]``) to use as | ||
| the center of a geospatial query. | ||
|
|
@@ -973,6 +973,26 @@ Geospatial Commands | |
| by multiplying by the radius of the | ||
| Earth. | ||
|
|
||
| :field box: Specifies a rectangle or box shape to query within. To | ||
| specify the bounds of the rectangle, you must specify | ||
| the bottom left and top right corners of the rectangle | ||
| in an array object. | ||
|
|
||
| .. TODO is this really 'circle'??? | ||
|
Contributor
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. can you confirm? |
||
|
|
||
| :field circle: Specifies a circular shape to query within. To | ||
| specify the bounds of the circle, you must specify | ||
| the center point in an array and the radius of the circle. | ||
|
|
||
| :field polygon: Specifies a polygon shape to query within. To | ||
| specify the bounds of the polygon, you must specify | ||
| an array of points. The last point will | ||
| automatically be joined with the first one. | ||
|
|
||
| :option boolean spherical: Specifies whether to use spherical | ||
| coordinate system to calculate | ||
| distances. | ||
|
Contributor
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. each of these use the word specify or specifies three times. I think we can say these things less tautologically, and more clearly. Give a revision stab, and ask me if you have questions.
Author
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. these are not options to geoNear (double checked the wiki). So removed. |
||
|
|
||
|
Contributor
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. this looks incomplete. |
||
| .. read-lock, slave-ok | ||
|
|
||
| .. dbcommand:: geoSearch | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,62 +271,138 @@ 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 and the relevant expressions follow: | ||
|
Contributor
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. "relevant expressions follow" is unnecessary. |
||
|
|
||
| - 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 box or rectangle shape | ||
|
Contributor
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. boxes are rectangle shapes |
||
| 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 | ||
|
Contributor
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?
Contributor
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?
Author
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?
Contributor
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: | ||
|
|
||
| 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. | ||
| - the center point, and | ||
|
|
||
| - Circles. Specify circles in the following form: | ||
| - the radius | ||
|
|
||
| .. code-block:: javascript | ||
| Considering the following example: | ||
|
|
||
| db.collection.find( { location: { $within: { $circle: [ center, radius } } } ); | ||
| .. code-block:: javascript | ||
|
|
||
| db.collection.find( { location: { $within: { $center: [ [0,0], 10 } } } ); | ||
|
|
||
| The above command returns all the documents that fall within a | ||
| 10 unit radius of the point [0,0]. | ||
|
Contributor
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. replace throughout as needed |
||
|
|
||
| - Polygons. Specify polygons with an array of points. See the | ||
| following example: | ||
| .. operator:: $polygon | ||
|
|
||
| .. code-block:: javascript | ||
| The :operator:`$polygon` operator specifies a polygon shape for | ||
| the :operator:`$within` operator. To define the bounds of the | ||
| polygon, you must specify an array of coordinate points. The | ||
| last point is always implicitly connected to the first | ||
| point. You can specify an arbitrary number of points to create | ||
| an multipoint polygon. See the following example: | ||
|
Contributor
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. |
||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.collection.find( { location: { $within: { $box: [[100,120], [100,100], [120,100], [240,200]] } } } ); | ||
| db.collection.find( { loc: { $within: { $polygon: [ [0,0], [3,6], [6,0] ] } } } ) | ||
|
|
||
| The last point of a polygon is implicitly connected to the first | ||
| point. | ||
| This will return all the documents that are within the polygon | ||
| [0,0], [3,6], [6,0]. This is useful for searching within | ||
|
Contributor
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. Show don't tell. Just provide people with the example, and its utility will be apparent (or people won't care in which case you've save them the time it takes to read 4 words.) Also I think it's important that we disambiguate searching from querying. |
||
| specific areas that correspond to irregularly shaped areas such | ||
| as a dense urban neighborhood. | ||
|
Contributor
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. the density of an urban neighborhood doesn't describe its shape. |
||
|
|
||
| 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 returns all documents near a | ||
| point using spherical representation system to calculate distances. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.collection.find( { loc: { $nearSphere: [0,0] } } ) | ||
|
|
||
| This will return all documents near [0,0] using a spherical | ||
| representation system to calculate distances from [0,0]. | ||
|
Contributor
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. coordinates need to be in-line literals |
||
|
|
||
| .. operator:: $centerSphere | ||
|
|
||
| The :operator:`$centerSphere` operator returns all the documents that | ||
| are within a certain radius from a point using spherical | ||
| representation. To define the bounds of the :operator:`$centerSphere` operator, | ||
| you must specify the center point and the radius. | ||
|
Contributor
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.
It's totally appropriate to cross reference here.
Contributor
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. make the above change throughout. |
||
|
|
||
| Considering the following example: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.collection.find( { loc: { $centerSphere: { [0,0], 10 / 3959 } } } ) | ||
|
|
||
| This will return all documents with a coordinate pair that is | ||
|
Contributor
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. This query returns all documents that have a coordinate pair in the |
||
| within a 10 mile radius of [0,0] using a spherical representation | ||
| system to calculate distances from [0,0]. | ||
|
Contributor
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. Throughout. |
||
|
|
||
| .. note:: | ||
|
|
||
| For spherical queries, you must convert distances to radians for | ||
| proper results. | ||
|
Contributor
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. cross reference in this section. |
||
|
|
||
| .. 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. | ||
| The :operator:`$uniqueDocs` operator overrides default query | ||
| behavior when :ref:`multiple location documents | ||
| <geospatial-query-multi-location>` are used. | ||
|
|
||
| .. TODO check this functionality again. | ||
|
|
||
| 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 specifying ``$uniqueDocs: false`` in the :operator:`$within` | ||
| operator, matching documents may be returned multiple times if | ||
| there are multiple matches. | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.places.find( { loc : { $within : { $center : [[0.5, 0.5], 20], $uniqueDocs : true } } } ) | ||
|
|
||
| When using the :dbcommand:`geoNear` command, if a document contains | ||
| more than one field with coordinate values, MongoDB will return the | ||
| same document multiple times. | ||
|
|
||
| If you specify ``uniqueDocs: true`` as an option to the | ||
| :dbcommand:`geoNear` command, then MongoDB will only return a | ||
| single document, even if there are multiple matches. | ||
|
Contributor
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. as in the following operation: |
||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.runCommand( { geoNear : "collection" , near : [0,0], num : 10, uniqueDocs : false } ) | ||
|
|
||
| .. note:: | ||
|
|
||
| The :operator:`$uniqueDocs` operator overrides these default | ||
| behaviors. | ||
| You cannot specify :operator:`$uniqueDocs` with | ||
| :operator:`$near` or haystack queries. | ||
|
Contributor
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 haystack indexes or haystack queries? this should cross reference. |
||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| You cannot specify :operator:`$uniqueDocs` with :operator:`$near` | ||
| or haystack queries. | ||
|
|
||
| .. 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.
we should avoid using parentheticals like this.