-
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 8 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,133 @@ 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` command supports three basic shape types. These | ||
|
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. not a command. |
||
| 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 | ||
| This specifies a box or rectangle shape for :operator:`$box`. To | ||
|
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.
|
||
| use the :operator:`$box` shape, you need to declare the bottom | ||
| left and top right corners of the box in an array | ||
|
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. s/box/rectangle/ |
||
| 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]. | ||
|
|
||
| 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. | ||
| .. 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... |
||
| - Circles. Specify circles in the following form: | ||
| This specifies a circle shape for :operator:`$within`. To use | ||
| the :operator:`$center` option, you need to specify the 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. it's not an option reprhase: To define the bounds of a query using :operator:
|
||
| point and the radius of the circle. Considering the following | ||
| example: | ||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| .. code-block:: javascript | ||
| db.collection.find( { location: { $within: { $center: [ [0,0], 10 } } } ); | ||
|
|
||
| db.collection.find( { location: { $within: { $circle: [ center, radius } } } ); | ||
| This will return all the documents that are 10 around point | ||
|
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. are 10 what?
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. it's upto what is in the coordinate points index setup. From the wiki:
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. Sorry, I thought it would be rhetorical: The units isn't clear. "around" only evokes "radius" in the vaguest sense. I think the better formulation is: |
||
| [0,0]. | ||
|
|
||
| - Polygons. Specify polygons with an array of points. See the | ||
| following example: | ||
| .. note:: | ||
|
|
||
| .. code-block:: javascript | ||
| Distances in queries are the same units as the location | ||
| index. | ||
|
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 "location index" is not a term we've used or defined anywhere. |
||
|
|
||
| db.collection.find( { location: { $within: { $box: [[100,120], [100,100], [120,100], [240,200]] } } } ); | ||
| :operator:`$center` provides greater fidelity in searching a | ||
| specific area than :operator:`$near` with :operator:`maxDistance` | ||
| options. | ||
|
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.
greater fidelity? this implies that it has higher resolution, when what I think is really going on, is it's more accurate around the edges? |
||
|
|
||
| .. operator:: $polygon | ||
|
|
||
| This specifies a polygon shape for :operator:`$within`. You can | ||
|
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. avoid use of the word "this" |
||
| specify an arbitrary number of points to create an multipoint | ||
| polygon. To specify a :operator:`$polygon` shape, use an array of | ||
| points. The last point is always implicitly connected to the | ||
| first point. See the following example: | ||
|
|
||
| .. 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] ] } } } ) | ||
|
|
||
| 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` option returns all documents near a | ||
|
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. not an option |
||
| 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` option returns all the documents that | ||
|
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. not an option |
||
| are within a certain radius from a point. Remember that all | ||
| distances, like the radius, have to be converted to radians. | ||
|
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. general admonitions (e.g. "remember...") are clumsy in these situations. Better to:
|
||
|
|
||
| .. code-block:: javascript | ||
|
|
||
| db.collection.find( { loc: { $centerSphere: { [0,0], 10 / 3959 } } } ) | ||
|
|
||
| This will return all documents within a 10 mile radius from [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. documents with a coordinate pair that is within... also things are within the radius of something, not from something. |
||
|
|
||
| .. 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 behaviors in | ||
| the :dbcommand:`geoNear` and :func:`find() <db.collection.find()>` | ||
| method with :operator:`$within` when there are multiple location | ||
| fields in documents (i.e. embedded in an array) | ||
|
|
||
| The :operator:`$uniqueDocs` operator overrides these default | ||
| behaviors. | ||
| When using the :operator:`$within`, if a document contains more | ||
|
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 $within what? |
||
| than one field with location coordinates, MongoDB will only return the | ||
| document once. | ||
|
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. isn't it that if the document appears in the result set more than once (due to hashing/sorting/query practices) the limitation you describe is really an elaboration of a general limitation of geo indexes |
||
|
|
||
| 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. | ||
| query, it will cause the query to return a single document multiple | ||
| times, even 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. | ||
| .. 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:: | ||
|
|
||
| You cannot specify :operator:`$uniqueDocs` with :operator:`$near` | ||
| or haystack queries. | ||
| 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. |
||
|
|
||
| .. 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.