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

Closed
jl33-ai opened this issue Jan 19, 2025 · 2 comments
Closed

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

jl33-ai opened this issue Jan 19, 2025 · 2 comments
Labels
developer-experience This issue improves error messages, debugging, or reporting
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
@vkarpov15
Copy link
Collaborator

This error message is generated on the MongoDB server, and it looks like you already opened a ticket on MongoDB's JIRA: https://jira.mongodb.org/browse/SERVER-99611.

We won't fix this issue for await User.aggregate([...]) because Mongoose doesn't do any validation on aggregation pipeline when you call aggregate(). But we do some validation when using the aggregate chaining API, so #15206 makes it so that the following will throw a more helpful error without sending any commands to the MongoDB server

        await User.aggregate().near({
            near: {
                type: 'Point',
                coordinates: [],
            },
            distanceField: 'distance',
            spherical: true,
            maxDistance: 1337,
            key: 'location',
        });

Does that help?

@vkarpov15 vkarpov15 added developer-experience This issue improves error messages, debugging, or reporting and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 27, 2025
@jl33-ai
Copy link
Author

jl33-ai commented Jan 27, 2025

Yep, I realized my mistake but wasn't sure whether to remove this issue.

Validation on the aggregate pipeline looks good. Thank you for informing me.

vkarpov15 added a commit that referenced this issue Jan 30, 2025
fix(aggregate): improve error when calling `near()` with invalid coordinates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience This issue improves error messages, debugging, or reporting
Projects
None yet
Development

No branches or pull requests

2 participants