Saturday, June 12, 2021

Support for GeoNear Has been removed

Starting in version 4.2, MongoDB removes the geoNear command. We need to use the $geoNear aggregation stage instead.


The options for $geoNear are similar to the removed geoNear command with the following exceptions:


The removed geoNear command includes in its output a field named dis that included the distance information.


For the $geoNear stage, specify the distance field name in distanceField.


The removed geoNear command accepts a boolean value for the includeLocs option to include the loc field.


For the $geoNear stage, specify the location field name in includeLocs.


The removed geoNear command includes the avgDistance and maxDistance of the returned results.


You can use the aggregation pipeline to return the avgDistance and maxDistance as well. Specifically, after the $geoNear stage, include a $group stage to calculate the avgDistance and maxDistance:


db.places.aggregate([

   { $geoNear: { near: <...>, distanceField: "dis", includeLocs: "loc", spherical: true, ... } },

   { $group: { _id: null, objectsLoaded: { $sum: 1 }, maxDistance:

         { $max: "$dis" }, avgDistance: { $avg: "$dis" } } }

])


references:

https://docs.mongodb.com/manual/release-notes/4.2-compatibility/#remove-support-for-the-geonear-command





No comments:

Post a Comment