A geospatial query in MongoDB is a way to find documents based on their location data, such as points on a map, using coordinates like latitude and longitude.
Click to reveal answer
beginner
Which MongoDB index type is used for 2D geospatial queries?
The '2d' index type is used for flat, two-dimensional geospatial queries on legacy coordinate pairs.
Click to reveal answer
beginner
What does the $near operator do in a geospatial query?
The $near operator finds documents with locations closest to a specified point, sorting results by distance from that point.
Click to reveal answer
intermediate
Explain the difference between '2d' and '2dsphere' indexes in MongoDB.
'2d' indexes work with flat coordinate systems for simple x,y points, while '2dsphere' indexes support spherical geometry for Earth-like curved surfaces and GeoJSON data.
Click to reveal answer
intermediate
What is GeoJSON and why is it important in MongoDB geospatial queries?
GeoJSON is a format for encoding geographic data structures like points, lines, and polygons. MongoDB uses GeoJSON with '2dsphere' indexes to perform accurate spherical geospatial queries.
Click to reveal answer
Which MongoDB operator finds documents near a specific point?
A$geoWithin
B$near
C$geoIntersects
D$match
✗ Incorrect
$near finds documents closest to a given point, sorted by distance.
What type of index should you create for spherical Earth data in MongoDB?
A2d
Btext
C2dsphere
Dhashed
✗ Incorrect
2dsphere indexes support spherical geometry and GeoJSON data.
Which format does MongoDB use to store complex geospatial shapes like polygons?
AGeoJSON
BXML
CCSV
DJSON
✗ Incorrect
GeoJSON is the standard format for geographic shapes in MongoDB.
What does the $geoWithin operator do?
AFinds documents near a point
BSorts documents by distance
CFinds documents intersecting a line
DFinds documents within a specified shape
✗ Incorrect
$geoWithin finds documents located inside a given geometry like a polygon.
Which index type is best for legacy flat coordinate pairs?
A2d
B2dsphere
Ctext
Dhashed
✗ Incorrect
2d indexes are designed for flat, two-dimensional coordinate pairs.
Describe how MongoDB uses geospatial indexes to perform location-based queries.
Think about how maps can be flat or round like the Earth.
You got /3 concepts.
Explain the role of GeoJSON in MongoDB geospatial queries and why it matters.
GeoJSON is like a language for describing shapes on maps.
You got /3 concepts.
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?