0
0
MongoDBquery~20 mins

Geospatial queries basics in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Geospatial Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Find documents within a circle
Given a MongoDB collection places with documents containing a location field as GeoJSON points, what will be the output of this query?

db.places.find({ location: { $geoWithin: { $centerSphere: [[-73.97, 40.77], 0.0001] } } })

The query finds documents within a circle centered at longitude -73.97, latitude 40.77 with radius 0.0001 radians.
ADocuments with locations roughly within 637 meters of the point (-73.97, 40.77)
BDocuments with locations exactly at (-73.97, 40.77) only
CDocuments with locations within 0.01 degrees of the point (-73.97, 40.77)
DDocuments with locations anywhere in the collection
Attempts:
2 left
πŸ’‘ Hint
Remember that $centerSphere uses radians for radius, not degrees or meters.
🧠 Conceptual
intermediate
1:30remaining
Understanding 2dsphere index usage
Which statement about MongoDB's 2dsphere index is correct?
AIt is used only for legacy coordinate pairs, not GeoJSON.
BIt automatically converts all coordinates to degrees before indexing.
CIt supports queries on GeoJSON objects and calculates distances on a spherical surface.
DIt only supports flat 2D plane calculations and cannot handle GeoJSON points.
Attempts:
2 left
πŸ’‘ Hint
Think about how Earth’s curvature affects distance calculations.
πŸ“ Syntax
advanced
2:00remaining
Identify the syntax error in a geospatial query
Which option contains a syntax error in the MongoDB geospatial query?
MongoDB
db.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, $maxDistance: 1000 } } })
Adb.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, maxDistance: 1000 } } })
Bdb.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, $maxDistance: 1000 } } })
Cdb.places.find({ location: { $near: { $geometry: { type: "Point", coordinates: [40.77, -73.97] }, $maxDistance: 1000 } } })
Ddb.places.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-73.97, 40.77] }, $maxDistance: 1000 } } })
Attempts:
2 left
πŸ’‘ Hint
Check the spelling and syntax of the distance option inside $near.
❓ optimization
advanced
1:30remaining
Optimizing geospatial queries with indexes
You have a collection restaurants with a location field storing GeoJSON points. Which index will optimize queries that find restaurants near a given point?
A{ location: "2d" }
B{ location: "2dsphere" }
C{ location: "hashed" }
D{ location: 1 }
Attempts:
2 left
πŸ’‘ Hint
Consider the type of geospatial data stored and the query type.
πŸ”§ Debug
expert
2:30remaining
Why does this geospatial query return no results?
You run this query on a MongoDB collection parks with a location field indexed as 2dsphere:

db.parks.find({ location: { $geoWithin: { $box: [[-74, 40], [-73, 41]] } } })

But it returns no documents, even though you know some parks are inside that box. What is the most likely reason?
AThe $geoWithin operator requires $centerSphere, not $box.
BThe coordinates in $box are reversed; longitude and latitude are swapped.
CThe collection does not have any documents with location fields.
DThe $box operator only works with 2d indexes, not 2dsphere indexes.
Attempts:
2 left
πŸ’‘ Hint
Check compatibility of operators with index types.