Find Restaurants with Geospatial Queries
MongoDB’s geospatial indexing allows you to efficiently execute spatial queries on a collection that contains geospatial shapes and points. This tutorial will briefly introduce the concepts of geospatial indexes, and then demonstrate their use with
To showcase the capabilities of geospatial features and compare different approaches, this tutorial will guide you through the process of writing queries for a simple geospatial application.
Suppose you are designing a mobile application to help users find restaurants in New York City. The application must:
- Determine the user’s current neighborhood using
- Show the number of restaurants in that neighborhood using
- Find restaurants within a specified distance of the user using
This tutorial will use a
2dsphere index to query for this data on spherical geometry.
For more information on spherical and flat geometries, see Geospatial Models.
Spherical geometry will appear distorted when visualized on a map due to the nature of projecting a three dimensional sphere, such as the earth, onto a flat plane.
For example, take the specification of the spherical square defined by the longitude latitude points
(0,80). The following figure depicts the area covered by this region:
Download the example datasets from https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/neighborhoods.json and https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json . These contain the collections
After downloading the datasets, import them into the database:
Because this data is geographical, create a
2dsphere index on each collection using the
Inspect an entry in the newly-created
restaurants collection from within the
This query returns a document like the following:
This restaurant document corresponds to the location shown in the following figure:
Because the tutorial uses a
2dsphere index, the geometry data in the
location field must follow the GeoJSON format.
Now inspect an entry in the
This query will return a document like the following:
This geometry corresponds to the region depicted in the following figure:
Assuming the user’s mobile device can give a reasonably accurate location for the user, it is simple to find the user’s current neighborhood with
This query will return the following result:
You can also query to find all restaurants contained in a given neighborhood. Run the following in the
mongo shell to find the neighborhood containing the user, and then count the restaurants within that neighborhood:
This query will tell you that there are 127 restaurants in the requested neighborhood, visualized in the following figure:
To find restaurants within a specified distance of a point, you can use either
$centerSphere to return results in unsorted order, or
$maxDistance if you need results sorted by distance.
To find restaurants within a circular region, use
$centerSphere is a MongoDB-specific syntax to denote a circular region by specifying the center and the radius in radians.
$geoWithin does not return the documents in any specific order, so it may show the user the furthest documents first.
The following will find all restaurants within five miles of the user:
$centerSphere’s second argument accepts the radius in radians, so you must divide it by the radius of the earth in miles. See Calculate Distance Using Spherical Geometry for more information on converting between distance units.