Skip to content

DOCS-934 centerSphere and within #654

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

Merged
merged 2 commits into from
Feb 15, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 19 additions & 17 deletions source/core/geospatial-indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
Overview
--------

``2d`` geospatial indexes support efficient queries using
location-based data in a document, and special geospatial query
operators. You can store two-dimensional location coordinates in
documents and with a geospatial index on this field, construct
location-based queries. For example, you can query for documents based
Geospatial indexing allows you to associate a document with a location
in two-dimensional space, such as a point on a map. You store
two-dimensional coordinates in a location field in your documents,
create a geospatial index on that field, and construct location-based
queries. Geospatial indexes provide special geospatial query operators.
For example, you can query for documents based
on proximity to another location or based on inclusion in a specified
region.

Additionally, geospatial indexes support queries on both the
coordinate field *and* another field. For example, you might write a
Geospatial indexes support queries on both the coordinate field *and*
another field, such as a type of business or attraction. For example,
you might write a
query to find restaurants a specific distance from a hotel or to find
museums found within a certain defined neighborhood.
museums within a certain defined neighborhood.

This document describes how to include location data in your documents
This document describes how to store location data in your documents
and how to create geospatial indexes. For information on querying data
stored in geospatial indexes, see :doc:`/applications/geospatial-indexes`.

Expand All @@ -31,17 +33,17 @@ Store Location Data

To use ``2d`` geospatial indexes, you must model location data on a
predetermined two-dimensional coordinate system, such as longitude
and latitude. You store location data as two-dimensional coordinates
and latitude. You store a document's location data as two coordinates
in a field that holds either a two-dimensional array or an embedded
document. Consider the following two examples:
document with two fields. Consider the following two examples:

.. code-block:: javascript

loc : [ x, y ]

loc : { x: 1, y: 2 }

All documents must store location data in the same order; however, if
All documents must store location data in the same order. If
you use latitude and longitude as your coordinate system, always store
longitude first. MongoDB's :ref:`2d spherical index operators
<geospatial-indexes-spherical>` only recognize ``[ longitude, latitude
Expand All @@ -63,7 +65,7 @@ location field of your collection. Consider the following prototype:

db.collection.ensureIndex( { <location field> : "2d" } )

MongoDB's special :ref:`geospatial operations
MongoDB's :ref:`geospatial operations
<geospatial-query-operators>` use this index when querying for location
data.

Expand All @@ -87,12 +89,12 @@ Location Range
~~~~~~~~~~~~~~

All ``2d`` geospatial indexes have boundaries defined by a coordinate
range. By default, ``2s`` geospatial indexes assume longitude and
range. By default, ``2d`` geospatial indexes assume longitude and
latitude have boundaries of -180 inclusive and 180 non-inclusive
(i.e. ``[-180, 180)``). MongoDB returns an error and rejects documents
with coordinate data outside of the specified range.

To build an index with a different location range other than the
To build an index with a location range other than the
default, use the ``min`` and ``max`` options with the
:method:`ensureIndex() <db.collection.ensureIndex()>` operation when
creating a ``2d`` index, as in the following prototype:
Expand Down Expand Up @@ -253,8 +255,8 @@ for geospatial information based on a sphere or earth.
.. admonition:: Spherical Queries Use Radians for Distance

For spherical operators to function properly, you must convert
distances to radians, and convert from radians to distances units
for your application.
distances to radians, and convert from radians to the distances units
used by your application.

To convert:

Expand Down
5 changes: 5 additions & 0 deletions source/includes/note-geospatial-index-must-exist.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
operators *without* having a geospatial index; however, geospatial
indexes will support much faster geospatial queries than the
unindexed equivalents.

.. note::

A geospatial index *must* exist on a field and the field must hold coordinates
before you can use any of the geolocation query operators.
22 changes: 15 additions & 7 deletions source/reference/operator/centerSphere.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ $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.
spherical geometry to define a circle for use by the
:operator:`$within` operator in :term:`geospatial` queries.

Considering the following example:
To define the bounds of a query using :operator:`$centerSphere`, you
must specify:

.. code-block:: javascript
- The center point

- The angular distance in radians (distance along the surface of the
earth).

db.collection.find( { loc: { $within: { $centerSphere: [ [0,0], 10 / 3959 ] } } } )
The following example returns all documents within a 10 mile radius
of longitude ``88 W`` and latitude ``30 N``. The distance (10 miles)
is converted to radians by dividing by the approximate radius of the
earth (3959 miles).

.. code-block:: javascript

This query will return all documents within a 10 mile radius of
``[0,0]`` using a spherical geometry to calculate distances.
db.collection.find( { loc: { $within: { $centerSphere: { [88,30], 10 /* miles */ / 3959 /* miles */ } } } } )

.. include:: /includes/note-geospatial-index-must-exist.rst
5 changes: 4 additions & 1 deletion source/reference/operator/within.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ $within
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
:operator:`$within` command supports the following shapes. These shapes and the
relevant expressions follow:

- Rectangles. Use the :operator:`$box` operator, consider the following
Expand All @@ -35,6 +35,9 @@ $within

db.collection.find( { location: { $within: { $center: [ center, radius } } } );

- Circular distance on a sphere. Use the
:operator:`$centerSphere` operator. For the syntax, see :operator:`$centerSphere`.

- Polygons. Use the :operator:`$polygon` operator. Specify polygons with an array of points. See the
following example:

Expand Down
27 changes: 27 additions & 0 deletions source/reference/operators.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ Geospatial
- :operator:`$near`
- :operator:`$within`

.. include:: operator/near.txt
:start-after: mongodb

.. include:: operator/nearSphere.txt
:start-after: mongodb

.. include:: operator/within.txt
:start-after: mongodb

Use the following operators within the context of
:operator:`$within`:

Expand All @@ -115,6 +124,24 @@ Use the following operators within the context of
- :operator:`$polygon`
- :operator:`$uniqueDocs`

.. include:: operator/maxDistance.txt
:start-after: mongodb

.. include:: operator/center.txt
:start-after: mongodb

.. include:: operator/centerSphere.txt
:start-after: mongodb

.. include:: operator/box.txt
:start-after: mongodb

.. include:: operator/polygon.txt
:start-after: mongodb

.. include:: operator/uniqueDocs.txt
:start-after: mongodb

.. index:: query selectors; array
.. _query-selectors-array:

Expand Down