MongoDB For Geospatial Data

Posted By : Arun Kataria | 30-Jan-2018

MongoDB can support the query operations on a geospatial data. Here is some feature of MongoDB’s geospatial:

Geospatial Data: MongoDB store geospatial data in a GeoJson object.

the format of GeoJson object is:

 {

  type: <GeoJSON type>,

  coordinates: <coordinates>

}
        

Here GeoJson type can be Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. And coordinates are in the array.
In case of Point as a GeoJson type then the coordinates would be latitude and longitude coordinates, Here longitude first and then latitude. Valid longitude values are between -180 to 180, both inclusive. And Valid latitude values are between -90 to 90, both inclusive.

Example: 

location: {

      type: "Point",

      coordinates: [-73.322037, 50.548557]

}
 

 

Now if you want to get the data by near by location you can use $near. $near specifies the point in which a geospatial query returns documents from neare to far. $near operator will specify either the GeoJSON point or legacy coordinate point.

example: 


db.user.find(

   {

     location:

       { $near :

          {

            $geometry: { type: "Point",  coordinates: [ -72.2627, 42.28 ] },

            $minDistance: 2000,

            $maxDistance: 6000

          }

       }

   }

);
 

 

here $minDistance and $maxDistance distance are in meters.

 

About Author

Author Image
Arun Kataria

Arun has good skills in AngularJS, NodeJS, MongoDB and many more. He is highly motivated which allow him to strive for proficiency when accomplishing assigned duties. He is an excellent team operative.

Request for Proposal

Name is required

Comment is required

Sending message..