$geoNear Aggregation Query Example Causing Strange Error

Hello Mongo team,

I have a $geoNear aggregation query which isn’t working for some reason. To check I was doing it correctly I copied the example code from the docs, and it still gives the following error:

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

This is query code:

const filterResults = await Listing.aggregate([
    {
        $geoNear: {
             near: { type: 'Point', coordinates: [parseFloat(lng), parseFloat(lat)] },
             distanceField: 'location.distance',
             maxDistance: 2000, //2km
             spherical: true,
        },
    },
    ...
])

In my Mongoose model/schema the field is defined like this:

location: {
        type: {
            type: String,
            enum: ['Point'],
            required: true,
        },
        coordinates: {
            type: [Number],
            required: true,
        },
    },

And indexed like this:

ListingSchema.index({ location: '2dsphere' })

I am using parseFloat(lng) etc. to ensure I’m passing numbers and not strings, and passing longitude first. In my collection the data is stored as [longitude, latitude] too.

Can anyone see what I’m doing wrong, please? Any help very much appreciated!
Cheers, Matt

The issue is likely related to the order of the coordinates in the $geoNear aggregation and/or the longitude/latitude inputs being in the wrong order. According to the MongoDB documentation, the longitude should be the first value and the latitude should be the second value when querying for a GeoJSON point. Make sure that the longitude is the first value of the coordinates array when defining the location in the model and also when passing the longitude and latitude arguments in the $geoNear query.
Assisted by Doc-E.ai
Reference: node.js - mongodb - using $geoNear gives "geo near accepts just one argument when querying for a GeoJSON point" when using maxDistance - Stack Overflow

Hi Deepak,
Thanks for your reply. I did write in the question, “I’m passing longitude first. In my collection the data is stored as [longitude, latitude] too”, which is why it’s so confusing. I’ll keep testing and report back when I find a solution.
Cheers,
Matt

@Matt_Heslington1
I got busy … spent some time today … let me know hope this is helpful …
The error message "MongoServerError: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 2000.0" suggests that the $geoNear stage only accepts one argument when querying for a GeoJSON point, and that an extra field $maxDistance was found.

Looking at the provided query code, it appears that the $maxDistance parameter is being incorrectly used. According to the MongoDB documentation, $maxDistance should be specified in meters as a number or in radians when using a legacy coordinate pair. In the given example, $maxDistance is set to 2000, which could be interpreted as 2000 meters. However, since the spherical option is set to true, the $maxDistance parameter should be in radians.

To fix the issue, try specifying $maxDistance in radians instead. One way to do this is by converting the distance in meters to radians using the formula distance in meters / earth radius in meters. For example, to set a $maxDistance of 2000 meters, use maxDistance: 2000 / 6371.1.

Modified query code with $maxDistance parameter specified in radians:

const filterResults = await Listing.aggregate([
    {
        $geoNear: {
             near: { type: 'Point', coordinates: [parseFloat(lng), parseFloat(lat)] },
             distanceField: 'location.distance',
             maxDistance: 0.0311, //2000 meters / 6371.1 earth radius in km = 0.0311 radians
             spherical: true,
        },
    },
    ...
])