Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-99611

$geoNear: Misleading error message when coordinates array is invalid

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Query Integration
    • ALL
    • Hide

       

      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.

      Show
        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.

      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

       

      Expected Behaviour

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

            Assignee:
            Unassigned Unassigned
            Reporter:
            hwyd4575@gmail.com Hwyd N/A
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: