Articles → MONGODB → Introduction To Geospatial Queries In Mongodb

Introduction To Geospatial Queries In Mongodb






What Is Geospatial?





What Are Geospatial Queries?





Shapes In Geospatial Queries


ShapesDescription
PointA single position.
LineStringA set of points linked by straight lines.
PolygonAn area enclosed by multiple points.
MultiPoint, MultiLineString, and MultiPolygonCollections of points, line strings, and polygons, respectively.
GeometryCollectionA collection of different geometric types.



Location Document Collection




Picture showing the sample mongodb collection





Creating An Index




db.locations.createIndex({ location: "2dsphere" });



Geospatial Query


db.locations.find({
  location: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [73.8567, 18.5204]
      },
      $maxDistance: 500  // Distance in meters
    }
  }
});



Output


Picture showing the output of geospatial query in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Monday, May 20, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250