📖 Scenario: You are building a simple location-based service that stores places with their geographic coordinates. You want to find places near a specific location.
🎯 Goal: Create a MongoDB collection with place data including coordinates, set up a geospatial index, and write a query to find places near a given point.
📋 What You'll Learn
Create a MongoDB collection named places with documents containing name and location fields.
The location field must be a GeoJSON point with exact coordinates.
Create a 2dsphere index on the location field.
Write a query using $near to find places near a specific coordinate.
💡 Why This Matters
🌍 Real World
Location-based services like finding nearby restaurants, parks, or stores use geospatial queries to provide relevant results based on user location.
💼 Career
Understanding geospatial queries is important for roles involving location data, such as backend developers, data engineers, and GIS specialists.
Progress0 / 4 steps
1
Create the places collection with place documents
Insert three documents into the places collection with these exact entries: { name: "Central Park", location: { type: "Point", coordinates: [-73.9654, 40.7829] } }, { name: "Times Square", location: { type: "Point", coordinates: [-73.9855, 40.7580] } }, and { name: "Empire State Building", location: { type: "Point", coordinates: [-73.9857, 40.7484] } }.
MongoDB
Hint
Use db.places.insertMany([...]) with the exact documents.
2
Create a 2dsphere index on the location field
Create a geospatial index on the location field of the places collection using db.places.createIndex({ location: "2dsphere" }).
MongoDB
Hint
Use db.places.createIndex({ location: "2dsphere" }) to enable geospatial queries.
3
Write a query to find places near a specific point
Write a query to find places near the coordinate [-73.9850, 40.7589] using db.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.9850, 40.7589] } } } }).
MongoDB
Hint
Use db.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [...] } } }) with the exact coordinates.
4
Assign the query result to a variable to complete the setup
Assign the result of the db.places.find query to a variable named nearbyPlaces to complete the geospatial query setup.
MongoDB
Hint
Assign the query to nearbyPlaces using const nearbyPlaces = db.places.find(...).
Practice
(1/5)
1. What does the $near operator do in MongoDB geospatial queries?
easy
A. Finds documents with locations closest to a specified point
B. Deletes documents within a certain distance
C. Updates documents with new coordinates
D. Sorts documents alphabetically by location name
Solution
Step 1: Understand the purpose of $near
The $near operator is used to find documents that are geographically close to a given point.
Step 2: Compare options with $near functionality
Only Finds documents with locations closest to a specified point correctly describes finding documents near a location. Other options describe unrelated actions.
Final Answer:
Finds documents with locations closest to a specified point -> Option A
Quick Check:
$near = Finds nearby points [OK]
Hint: Remember: $near finds closest points to a location [OK]
Common Mistakes:
Thinking $near deletes or updates documents
Confusing $near with sorting operators
Assuming $near works without a geospatial index
2. Which of the following is the correct syntax to find documents near a point using $near in MongoDB?
Coordinates must be in [longitude, latitude] order, so longitude comes first.
Step 2: Check each option's coordinate order
{ location: { $near: { $geometry: { type: 'Point', coordinates: [-70, 40] } } } } uses [-70, 40], which is [longitude, latitude]. Others have latitude first or wrong signs.
A. Coordinates are in wrong order; should be [longitude, latitude]
B. Missing $maxDistance parameter
C. The location field is not indexed with 2dsphere
D. The type should be 'Polygon' instead of 'Point'
Solution
Step 1: Requirements for $near with $geometry
The $near operator with $geometry requires a 2dsphere index on the location field.
Step 2: Identify the most likely error cause
Without a 2dsphere index, MongoDB throws an error for geospatial queries like $near. Coordinate order affects results but does not cause an error.
Final Answer:
The location field is not indexed with 2dsphere -> Option C
Quick Check:
$near requires 2dsphere index [OK]
Hint: Always put longitude before latitude in coordinates [OK]
Common Mistakes:
Swapping latitude and longitude
Assuming $maxDistance is required
Confusing geometry types
5. You want to find all restaurants within 1 km of a user's location stored as { type: 'Point', coordinates: [-122.42, 37.77] }. Which query correctly finds these restaurants assuming a 2dsphere index on location?