node.jsmongodbfeathersjsgeonear

How to use $geoNear using mongodb feathersjs without mongoose


I am trying featherjs for the first time. And stuck on how to use $geoNear to fliter by geo location. I am not using mongoose, only feathers-mongodb


Solution

  • This can be accomplished by accessing the MongoDB collection directly through app.service('myservice').Model and running the $geoNear aggregate:

    const results = await app.service('myservice').Model.aggregate([ {
        $geoNear: {
          includeLocs: "location",
          distanceField: "distance",
          near: {type: 'Point', coordinates: [lng, lat]},
          maxDistance: 1000,
          spherical: true
        }
    ]);
    

    You can set this as context.result in a hook or use it anywhere else you need it.