Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$geoNear: Misleading error message when coordinates array is invalid #15188

Open
jl33-ai opened this issue Jan 19, 2025 · 0 comments
Open

$geoNear: Misleading error message when coordinates array is invalid #15188

jl33-ai opened this issue Jan 19, 2025 · 0 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

Comments

@jl33-ai
Copy link

jl33-ai commented Jan 19, 2025

Mongoose version

^8.0.0

Description

The $geoNear aggregation stage produces a misleading error message when performing GeoJSON point queries with invalid coordinates. When the coordinates array in the near parameter is undefined or invalid (e.g. []), instead of indicating the coordinate validation issue, it incorrectly suggests there's a problem with having multiple arguments:

MongoServerError: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 1337.0

This error message is confusing because:

  1. The maxDistance parameter is valid and supported by $geoNear
  2. The actual issue is the invalid coordinates array in the query

Steps to Reproduce

import mongoose from 'mongoose';
import {MongoMemoryServer} from 'mongodb-memory-server';


const ExampleSchema = new mongoose.Schema({});
ExampleSchema.index({location: '2dsphere'});

const User = mongoose.model('User', ExampleSchema);

async function main() {
    let mongod;
    try {
        mongod = await MongoMemoryServer.create(); // same issue occurs with real connection
        const mongoUri = mongod.getUri();

        await mongoose.connect(mongoUri);

        // create a document
        await new User({
            location: {
                type: 'Point',
                coordinates: [0, 0]
            }
        }).save();

        await User.aggregate([
            {
                $geoNear: {
                    near: {
                        type: 'Point',
                        coordinates: [],
                    },
                    distanceField: 'distance',
                    spherical: true,
                    maxDistance: 1337,
                    key: 'location',
                },
            }
        ]);
    } catch (error) {
        console.error(error.stack);
    } finally {
        await mongoose.disconnect();
        if (mongod) {
            await mongod.stop();
        }
    }
}

main();
// will throw the following:
// MongoServerError: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 1337.0

Copy and paste the code into any project with Typescript and mongoose 8.x, npm i and then run the script with Node.

Note: The provided snippet uses an in-memory instance for ease of reproduction, but the behaviour is identical with a proper connection.

Expected Behaviour

The error message should clearly indicate that the coordinates array is invalid instead of suggesting the removal of a valid parameter (maxDistance).

@jl33-ai jl33-ai changed the title Incorrect error message when geoNear has invalid location $geoNear: Misleading error message when coordinates array is invalid Jan 19, 2025
@jl33-ai jl33-ai changed the title $geoNear: Misleading error message when coordinates array is invalid $geoNear: Misleading error message when coordinates array is invalid Jan 19, 2025
@vkarpov15 vkarpov15 added this to the 8.9.6 milestone Jan 20, 2025
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants