- Tutorials >
- Geospatial Search
Geospatial SearchΒΆ
MongoDB offers a number of indexes and query mechanisms to handle geospatial information. This section demonstrates how to create and use geospatial indexes with the Ruby driver.
The examples on this page use a sample collection called
restaurants
in the test
database.
A sample dataset
is available for download.
The following is a sample document in the restaurants
collection:
The following example creates a 2dsphere
index on the
address.coord
field:
Once the index is created, you can use several operators to query
against it, including the
$near,
$geoWithin, and
$geoIntersects
operators. The following example uses the $near
operator to find
all restaurants within 500 meters of the given coordinates.
To find all documents with a location within the
perimeter of a given polygon, use the $geoWithin
operator: