$geoNear (Geo Near)

Last updated: 2026-10-09

$geoNear (Geo Near)

$geoNear outputs documents ordered by geographic proximity and must be the first pipeline stage, requiring a geospatial index on the collection. It supports a distanceField output, spherical or planar math, minDistance and maxDistance filters, and a key to choose among multiple geo fields. Each result gains a computed distance.

CategoryAdvanced & Diagnostic
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $geoNear: { near: { type: "Point", coordinates: [ <longitude>, <latitude> ] }, distanceField: "<distanceField>", spherical: <bool>, maxDistance: <meters>, query: <filter> } }

⚙️ Parameters

nearThe reference point, a GeoJSON point or legacy coordinates array.
distanceFieldField name that receives the computed distance.
sphericalWhen true, computes distances using spherical geometry.
queryAdditional query filter.

Returns:Document stream ordered by distance, with the distanceField included

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $geoNear: places within 5 km
db.places.aggregate([
  { $geoNear: {
    near: { type: 'Point', coordinates: [-73.98, 40.75] },
    distanceField: 'dist',
    spherical: true,
    maxDistance: 5000
  } }
])

Output:

// Sorted by distance ascending, each doc carries dist

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat happens without a geospatial index?
AIt errors; the collection must have a 2dsphere or 2d geospatial index.
QCan results be filtered by distance?
AYes, via maxDistance and minDistance, in meters for spherical mode.
QCan additional query filters be added?
AYes, via the query parameter, e.g. { query: { category: 'restaurant' } }.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏