Filter on $geoNear (destination) and sort based on $geoNear( origin) Filter on $geoNear (destination) and sort based on $geoNear( origin) mongoose mongoose

Filter on $geoNear (destination) and sort based on $geoNear( origin)


I solved this issue by a different approach.

First I added the distance field to my data. Made origin and destination as separate object instead of array.

{ "_id" : ObjectId("5b5a9cd706f9b02068ebc4a6"), "name" : "Bangalore to hyderabad","origin" : {        "coordinates" : [            77.5945627,             12.9715987        ],         "_id" : ObjectId("5b5a9cd706f9b02068ebc4a8"),         "formattedAddress" : "Bengaluru, Karnataka, India",         "name" : "Bengaluru",         "type" : "Point",         "googlePlaceId" : "5b0d9fd719c9616d747b8a0d",     }, destination: {        "coordinates" : [            78.486671,             17.385044        ],         "_id" : ObjectId("5b5a9cd706f9b02068ebc4a7"),         "formattedAddress" : "Hyderabad, Telangana, India",         "name" : "Hyderabad",         "type" : "Point",         "googlePlaceId" : "5b0d9fd719c9616d747b8a0d",     }

And then I made my query to fire geoNear on origin field with 1KM radius and query of distance less then 200KM.

Ride.aggregate([    {        $geoNear: {            near: { type: "Point", coordinates: [ 77.5946,12.8716 ] },            distanceField: "distanceFromOrigin",            maxDistance: 1000, //1Km            query: { private: false, distance:{$lt:200}},            spherical: true        }    },    // { "$sort": { "distanceFromOrigin":1,"distance": 1 } },    { "$skip": 0 },    { "$limit": 30 }]);