Search code examples
javascriptnode.jsmongooseaggregationfeathersjs

How do I write an aggregate using mongoose feathers adapter?


I'm new to feathersjs framework and try to write aggregation query it's not working properly.

hook.app.query = { 
   lookup: {
      from: "orders",
      localField:"serviceLocationId",
      foreignField:"serviceLocationId",
      as: "orders" 
   },
   match: { serviceLocationId : { $in: Array.from(new Set(reqArr)) } },
   limit: 14 
}

hook.app.service('servicelocations')
    .find(hook.app.query)
    .then(result => {
        console.log(result)
    })

Solution

  • we need to use following way service.Model.aggregate then its working fine

    function locations(hook) {
        return new Promise((resolve,reject) =>{
            hook.app.service('location')
              .Model.aggregate(hook.app.query)
              .then(result => {
                resolve(result)
              }).catch(e=>{
                reject(e)
              })
          })
    }