Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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
8 changes: 6 additions & 2 deletions draft/applications/geospatial-indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ Circles

To return results within the :ref:`bounds <geospatial-query-bounded>`
of a circle, you must specify the center and the radius of the circle,
using the :operator:`$within` operator and the :operator:`$circle`
option. Consider the following prototype query:
using the :operator:`$within` operator and the :operator:`$center`
operator. Consider the following prototype query:

.. code-block:: javascript

Expand Down Expand Up @@ -364,6 +364,8 @@ their distance from the ``[ -74, 40.74 ]`` point.

.. TODO add in distanceMultiplier description

.. _geospatial-query-multi-location:

Multi-location Documents
------------------------

Expand Down Expand Up @@ -429,4 +431,6 @@ in the following example:

db.records.ensureIndex( { "addresses.loc": "2d" } )

.. TODO include: includeLocs field

.. includes:: /includes/geospatial-sharding.rst
24 changes: 22 additions & 2 deletions source/reference/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Copy link
Contributor

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.


:field near: Takes the coordinates (e.g. ``[ x, y ]``) to use as
the center of a geospatial query.
Expand All @@ -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'???
Copy link
Contributor

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

these are not options to geoNear (double checked the wiki). So removed.


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
146 changes: 111 additions & 35 deletions source/reference/operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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
The :operator:`$box` operator specifies a box or rectangle shape
Copy link
Contributor

Choose a reason for hiding this comment

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


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

Choose a reason for hiding this comment

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

 [0,0] = ``[0,0]``

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

Choose a reason for hiding this comment

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

 Use :operator:`$polygon`  to specify a polgon for a bounded
 query using the :operator:`$within` operator. You must specify 
 an array of coordinate points to define the bounds of a polygon, 
 as in the following:

      [ [ x,y ], [x,y], [x,y] ]

 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

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

Choose a reason for hiding this comment

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

  • you define the bounds of the shape for the query, not the operator.
  • the first sentence needs reworking:
    • the operator specifies the constraints of a query (say this in a different way,) but it in and for itself doesn't return anything.
    • the operator doesn't "using spherical representation," it assumes the points exist in a spherical system.

It's totally appropriate to cross reference here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

This query returns all documents that have a coordinate pair in the loc field.

within a 10 mile radius of [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.

[ 0 , 0 ] = ``[  0 , 0 ]``

Throughout.


.. 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 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.
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::

The :operator:`$uniqueDocs` operator overrides these default
behaviors.
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.


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