Over a million developers have joined DZone.

Geolocation APIs in MongoDB

DZone's Guide to

Geolocation APIs in MongoDB

MongoDB comes with built-in geospatial indexing and search functionality. Read on to learn about how to initialize a geospatial index in MongoDB.

· Database Zone
Free Resource

Traditional relational databases weren’t designed for today’s customers. Learn about the world’s first NoSQL Engagement Database purpose-built for the new era of customer experience.

MongoDB is the NoSQL database known around the world for its clever document-based structure, ease of use, and flexibility. When some of the biggest companies in the world, like Forbes and Bosch, use MongoDB for their systems, you know that you are in good hands.

Unlike most other NoSQL databases, MongoDB comes with built-in geospatial indexing and search functionality, which makes it perfect for developers needing simple location based querying and map work.

Support for geospatial data is in the form of GeoJSON objects, which, if you are familiar with JSON, aren’t too difficult to wrap your head around. To initialize a geospatial index in MongoDB, we use a 2dsphere. You also have the option to use just a simple 2d flat geometry. However, this is not as precise as using the 2dsphere. These two types of indexes are designed specifically to return queries on the geospatial data contained efficiently.

To create a 2dsphere index, you need to create the index as such:

db.mycollection.createIndex( { <location field in collection> : "2dsphere" } )

You can do this before or after populating your collection of GeoJSON objects, either is acceptable and works.

The MongoDB 2dsphere index includes support for all the following GeoJSON objects: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. To add a GeoJSON object to the database, you need to specify in the following manner:

{ type: “<Type>”,  coordinates: <coordinates> }

In GeoJSON we must always remember that when inserting coordinates, longitude is written before latitude. This can cause many headaches if you mess it up!

For example:

{ type: “Point”, coordinates: [ 55.2, 7.5 ] }

So, for instance, we might choose to add public playgrounds to a collection, which would be done as such:



                              location : { type: “Point”, coordinates: [33.6, 23.5] }

                              name : “Shelley Playgrounds”




                              location : { type: “Point”, coordinates: [46.9, 37.3] }

                              name : “Wintergardens”


And if we haven’t already, create the 2dsphere index on our collection:

db.playgrounds.createIndex( “location” : “2dsphere” )

If you then have a collection of geospatial data in your database, there are some really neat queries that you can do on the data within it.

$geoWithin returns all GeoJSON objects within a given specified polygon. For instance, this call would return both of our playgrounds:

db.playgrounds.find( { “location” :

               { $geoWithin :

                              { $geometry : { type : "Polygon" ,  coordinates : [0, 0] , [0, 100], [100, 100], [100,0]  } }


} )

$geoIntersects checks to see if a GeoJSON object is intersected by a given geometry. This would return our “Wintergardens” playground:

db.playgrounds.find( { “location” :

               { $geoIntersects:

                              { $geometry : { type : "Polygon" ,  coordinates : [0, 0] , [0, 37.3], [100, 37.3], [100,0]  } }


} )

geoNear is a very handy feature that returns objects near to a given point. This command would return Wintergardens, followed by Shelley Playgrounds.

db.runCommand( { geoNear : “playgrounds” ,

              near : { type : "Point" , coordinates: [ 46, 36 ] } ,

spherical : true } )

There’s a lot that you can do with MongoDB and GeoJSON spatial data. We’ve only just touched on the basics today. For tutorials, you can check out the MongoDB site itself to get started.

Learn how the world’s first NoSQL Engagement Database delivers unparalleled performance at any scale for customer experience innovation that never ends.

mongodb ,tutorial ,nosql ,geolocation api

Published at DZone with permission of Raval Harsh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}