intermediateTop 30 Scenario-Based Questions
How do you implement geo-spatial queries in MongoDB for a location-based service?
MongoDB 2dsphere index for geo-queries: 1) Schema: {_id, name, location: {type:'Point', coordinates:[longitude, latitude]}}. 2) Index: db.stores.createIndex({location:'2dsphere'}). 3) Near query (sorted by distance): db.stores.find({location: {$near: {$geometry:{type:'Point', coordinates:[-73.9857,40.7484]}, $maxDistance:5000}}}). 4) Within polygon: db.areas.find({location:{$geoWithin:{$geometry:{
This is a Pro chapter
Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.
How do you implement geo-spatial queries in MongoDB for a location-based service?