Geolocation APIs in MongoDB

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 isn’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:db.playgrounds.insert(

{

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

name : “Shelley Playgrounds”

}

db.playgrounds.insert(

{

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.

Conclusion

Let's Talk
Lets Talk

Our Latest Blogs

With Zymr you can