When it comes to location data, MongoDB’s ability to work with GeoJSON through geospatial queries is often under-appreciated. Being able to query for intersecting or nearby coordinates while maintaining performance is functionality a lot of organizations are looking for.

Take the example of maintaining a list of business locations or even a fleet of vehicles. Knowing where these locations are, relative to a particular position isn’t an easy task when doing it manually.

In this tutorial we’re going to explore the $near operator within a MongoDB Realm application to find stored points of interest within a particular proximity to a position. These points of interest will be rendered on a map using the Mapbox service.

To get a better idea of what we’re going to accomplish, take the following animated image for example:

/images/how-to/mapbox-mongodb-points-of-interest.gifWe’re going to pre-load our MongoDB database with a few points of interest that are formatted using the GeoJSON specification. When clicking around on the map, we’re going to use the $nearoperator to find new points of interest that are within range of the marker.

The Requirements

There are numerous components that must be accounted for to be successful with this tutorial:

  • A MongoDB Atlas free tier cluster or better to store the data.
  • A MongoDB Realm application to access the data from a client-facing application.
  • A Mapbox free tier account or better to render the data on a map.

The assumption is that MongoDB Atlas has been properly configured and that MongoDB Realm is using the MongoDB Atlas cluster.

MongoDB Atlas can be used for FREE with a M0 sized cluster. Deploy MongoDB in minutes within the MongoDB Cloud.

In addition to Realm being pointed at the Atlas cluster, anonymous authentication for the Realm application should be enabled and an access rule should be defined for the collection. All users should be able to read all documents for this tutorial.

In this example, Mapbox is a third-party service for showing interactive map tiles. An account is necessary and an access token to be used for development should be obtained. You can learn how in the Mapbox documentation.

MongoDB Geospatial Queries and the GeoJSON Data Model

Before diving into geospatial queries and creating an interactive client-facing application, a moment should be taken to understand the data and indexes that must be created within MongoDB.

Take the following example document:

{
    "_id": "5ec6fec2318d26b626d53c61",
    "name": "WorkVine209",
    "location": {
        "type": "Point",
        "coordinates": [
            -121.4123,
            37.7621
        ]
    }
}

copy code

Let’s assume that documents that follow the above data model exist in a location_servicesdatabase and a points_of_interest collection.

To be successful with our queries, we only need to store the location type and the coordinates. This location field makes up a GeoJSON feature, which follows a specific format. The name field, while useful isn’t an absolute requirement. Some other optional fields might include an addressfield, hours_of_operation, or similar.

Before being able to execute the geospatial queries that we want, we need to create a special index.

#mongodb #mapbox

Searching for Nearby Points of Interest with MongoDB and Mapbox
2.10 GEEK