meteor/meteor · error · MiniMongoQueryError
$near argument must be coordinate pair or GeoJSON
Error message
$near argument must be coordinate pair or GeoJSON
What it means
Thrown by the $near operator in legacy coordinate-pair mode when the operand is not indexable as a coordinate pair. $near supports two forms: GeoJSON (operand has $geometry) and legacy [x, y] arrays. The else branch calls isIndexable(operand); if it is not a valid coordinate pair, the error is raised before pointToArray.
Source
Thrown at packages/minimongo/common.js:440
return GeoJSON.pointDistance(
point,
{type: 'Point', coordinates: pointToArray(value)}
);
}
if (value.type === 'Point') {
return GeoJSON.pointDistance(point, value);
}
return GeoJSON.geometryWithinRadius(value, point, maxDistance)
? 0
: maxDistance + 1;
};
} else {
maxDistance = valueSelector.$maxDistance;
if (!isIndexable(operand)) {
throw new MiniMongoQueryError('$near argument must be coordinate pair or GeoJSON');
}
point = pointToArray(operand);
distance = value => {
if (!isIndexable(value)) {
return null;
}
return distanceCoordinatePairs(point, value);
};
}
return branchedValues => {
// There might be multiple points in the document that match the given
// field. Only one of them needs to be within $maxDistance, but we need to
// evaluate all of them and use the nearest one for the implicit sort
// specifier. (That's why we can't just use ELEMENT_OPERATORS here.)View on GitHub (pinned to 5076d2f818)
Solutions
- For legacy pairs, pass a two-number array in [longitude, latitude] order: {field: {$near: [lng, lat], $maxDistance: 1000}}.
- For GeoJSON, wrap the geometry in $geometry: {field: {$near: {$geometry: {type: 'Point', coordinates: [lng, lat]}, $maxDistance: 1000}}}.
- Validate the operand is a 2-element numeric array or contains $geometry before building the selector.
Example fix
// before
collection.find({ loc: { $near: { lat: 40, lng: -73 } } });
// after
collection.find({ loc: { $near: [-73, 40], $maxDistance: 1000 } }); Defensive patterns
Strategy: validation
Validate before calling
function nearSelector(operand, maxDistance) {
const isPair = Array.isArray(operand) && operand.length === 2 && operand.every(Number.isFinite);
const isGeoJSON = operand && typeof operand === 'object' && '$geometry' in operand;
if (!isPair && !isGeoJSON) {
throw new TypeError('$near needs [lng,lat] or {$geometry: GeoJSON}');
}
const sel = { $near: operand };
if (maxDistance !== undefined) sel.$maxDistance = maxDistance;
return sel;
} Type guard
function isCoordinatePair(op) {
return Array.isArray(op) && op.length === 2 && op.every(v => typeof v === 'number' && Number.isFinite(v));
}
function isGeoJSONNear(op) {
return op !== null && typeof op === 'object' && '$geometry' in op;
} Prevention
- Pass [longitude, latitude] two-number arrays for legacy $near.
- Wrap GeoJSON in $geometry for 2dsphere queries.
- Validate operand shape at the selector-builder boundary.
When it happens
Trigger: Using {field: {$near: 'point'}}, {field: {$near: 5}}, {field: {$near: {lat:1,lng:2}}} (object without $geometry), or {field: {$near: [1]}} (single-element array) in a minimongo query.
Common situations: Passing a GeoJSON-like object but omitting the $geometry wrapper; passing lat/lng as a plain object instead of [lng, lat]; passing fewer/more than two coordinates; confusing latitude-first vs longitude-first ordering.
Related errors
- $maxDistance needs a $near
- $near can't be inside another $ operator
- argument to $mod must be an array of two numbers
- $in needs an array
- cannot nest $ under $in
AI-assisted analysis of meteor/meteor@5076d2f818 (2026-08-13).
Data as JSON: /api/errors/c0c2237cbe900c7d.
Report an issue: GitHub.