$geoNear Aggregation Query Example Causing Strange Error

@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,
        },
    },
    ...
])