Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions source/reference/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,16 @@ Geospatial Commands
by multiplying by the radius of the
Earth.

.. TODO add in box, circle, polygon options

:field box: box operator details

:field circle: circle operator details

:field polygon: polygon details

:field spherical: spherical details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks incomplete.

.. read-lock, slave-ok

.. dbcommand:: geoSearch
Expand Down
139 changes: 105 additions & 34 deletions source/reference/operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a command.

shapes and the relevant expressions follow:
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • this
  • "for $box?"

use the :operator:`$box` shape, you need to declare the bottom
left and top right corners of the box in an array
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it circle or center?

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/a-leung/mongodb-docs/blob/c2fda9f17af49cd182e4420fa2d2e276d4fdd5db/draft/applications/geospatial-indexes.txt#L239

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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:$center you must specify

  • foo
  • bar

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are 10 what?

Copy link
Author

Choose a reason for hiding this comment

The 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:
All distances in geospatial queries are specified in the same units as the document coordinate system (aside from spherical queries, discussed below ). For example, if your indexed region is of size [300, 300), representing a 300 x 300 meter field, and you have documents at locations (10, 20) and (10, 30), representing objects at points in meters (x, y), you could query for points $near : [10, 20], $maxDistance : 10. The distance unit is the same as in your coordinate system, and so this query looks for points up to 10 meters away.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I thought it would be rhetorical:

 "This will return the documents that are 10 around point [0,0]" 

The units isn't clear. "around" only evokes "radius" in the vaguest sense. I think the better formulation is:

"The above operation returns all documents with coordinates 
that fall within a 10 unit radius of the point `[0,0]`." 

[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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxDistance as such isn't an operator.

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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].
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general admonitions (e.g. "remember...") are clumsy in these situations. Better to:

  • give a link to the spherical section that covers this in great depth.
  • say "The distance returned by this query is in radians, you must convert to the units of your choice in your application" (edit this as needed, but you get the idea.)
  • put some or all of this after the example, not before


.. 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].
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Expand Down