SQL and data types · Engineering
GeoJSON point indexes and geospatial queries
Point indexes, $near distances, $geoWithin shapes, and the limits of a small geospatial implementation.
Location search is often a small feature inside a larger application: find nearby stores, filter a region, or check whether a point falls inside a service area. NonoDB supports a focused geospatial subset based on GeoJSON points.
It is not a GIS engine. The useful question is whether the subset covers the common “near me” and “inside this area” queries without adding another database.
The data model
A geo index is a single-field index with geo: true:
{"op": "create_index", "coll": "places", "field": "loc", "geo": true}Documents use a GeoJSON Point:
{"_id": "waw", "loc": {"type": "Point", "coordinates": [21.01, 52.23]}}Coordinates are longitude, latitude. The index is maintained transactionally on insert, update, and delete.
Geo indexes must have exactly one field and cannot be unique or covering. They index points, not polygons or lines.
$near
$near and $nearSphere return matches ordered by distance:
{ "op": "find", "coll": "places", "filter": { "loc": { "$near": { "$geometry": {"type": "Point", "coordinates": [21.01, 52.23]}, "$minDistance": 0, "$maxDistance": 5000 } } }, "limit": 20}Distances are in meters and use a spherical Earth model. With $maxDistance, a matching geo index narrows the candidate grid cells. Without $maxDistance, the query still returns distance order but falls back to a collection scan.
$near cannot be combined with an explicit sort; distance is the sort.
$geoWithin
$geoWithin accepts several shapes:
{"loc": {"$geoWithin": {"$box": [[20.0, 51.0], [22.0, 53.0]]}}}{"loc": {"$geoWithin": {"$centerSphere": [[21.01, 52.23], 0.01]}}}{"loc": {"$geoWithin": {"$polygon": [[20, 51], [22, 51], [22, 53], [20, 53]]}}}{"loc": {"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": [[[20,51],[22,51],[22,53],[20,53],[20,51]]]}}}}$box uses a rectangle. $centerSphere uses radians for its radius. $polygon uses a list of positions. $geometry accepts a GeoJSON geometry.
$geoIntersects accepts {"$geometry": ...} and matches point documents against the supplied geometry.
Point-in-polygon checks use planar lon/lat geometry. Distance checks use haversine meters. That is accurate for ordinary city and region queries but not a full spherical topology engine.
Index behavior
A geo index uses a hierarchy of longitude/latitude grid cells. Large queries may choose a coarser level, so the residual filter always verifies the exact result. The index narrows candidates; it does not replace the final geometry check.
This is a common pattern in small geo implementations: the index is a coarse spatial filter, and the exact predicate is evaluated on the candidate set. It keeps memory bounded and avoids a separate R-tree service.
What is missing
- Only GeoJSON
Pointdocuments are indexed. - Polygon and MultiPolygon geometries are supported in query arguments, not as indexed shapes.
- There is no
$geoNearaggregation stage; use a normal find with$near. - There is no full GIS topology, projections, or coordinate-system transform.
- Large-scale spatial joins are not a design goal.
If you need routing, complex polygon intersections over millions of shapes, or a mature GIS query language, use PostGIS. If you need “find the nearest five stores” or “which district contains this point”, the built-in point index is often enough.
Practical checklist
A geo index is a normal secondary index in one respect: create it before the query becomes hot, and check explain to see whether it was selected. A query without $maxDistance can still return distance order without index narrowing, which may be fine for a small collection but misleading at scale.
- Store coordinates as
[lon, lat], not[lat, lon]. - Create a geo index on the point field.
- Use
$maxDistancewhen you want index narrowing. - Do not combine
$nearwithsort. - Test at the antimeridian and near the poles if your data goes there.
- Remember that distance is spherical, while point-in-polygon is planar.
The geo API section has the exact query forms and limits.