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


  1. Point → A single position
  2. LineString → A series of points connected by straight lines
  3. Polygon → A closed area defined by multiple points
  4. MultiPoint, MultiLineString, and MultiPolygon→Collections of points, line strings, and polygons, respectively
  5. GeometryCollection → A 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