Skip to main content

What are the trade-offs of using $near for geospatial queries?

Senior MongoDB
Quick Answer $near and $geoNear for geospatial queries require a 2dsphere or 2d index. Performance trade-offs: they sort results by distance (expensive for large result sets), can't be combined efficiently with other indexes (geospatial index used, other filters applied after), and $near doesn't work in aggregation pipelines ($geoNear must be first stage). Limit results to a reasonable maxDistance and document count.

Answer

$near provides distance-ordered results but can be CPU-heavy and require specialized indexes for performance.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice