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:{
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response