Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb Nodejs driver aggregate query does not return data

I've Mongodb aggreage query which works fine in the RoboMongo shell and yeild me the correct results.

Robo Mongo Shell Query

db.getCollection('application-filters').aggregate(
{

      $match: { 


          "StatusName" : {$in:["Rejected","Expired"]}


}},
{
$group:{
    _id: "$StatusName", COUNT : { "$sum":1} 
}},
{
$project: {
    StatusName:1,
    Count : "$COUNT"
}
},
{
    $sort:{
        Count:-1
    }
}
)

I've copied and paste the same query and trying to execute with nodejs mongodb 2.2 driver. it gives me no result

Here is JavaScript code

module.exports = mPool => {
  return {
    getcountbyStatus (countstatusfilterParams) {
      console.log(countstatusfilterParams)
      return mPool.collection('application-filters').aggregate(
        {

          $match: {

            'StatusName': {$in: ['Rejected', 'Expired']}

          }},
        {
          $group: {
            _id: '$StatusName', COUNT: {'$sum': 1}
          }},
        {
          $project: {
            StatusName: 1,
            Count: '$COUNT'
          }
        },
        {
          $sort: {
            Count: -1
          }
        }
).toArray(function (err, data) {
  if (!err) {
    console.log(data)
  }
})
    }
  }
}

Any help will be highly appreciated.

Thanks

like image 594
MAQ Avatar asked Sep 16 '25 17:09

MAQ


1 Answers

Have you tried this:

db.collection.aggregate([
      // do your query
]).toArray(function(err, docs) {
      // do something
}

So in this case your Mongodb aggreage should be:

module.exports = mPool => {
  return {
    getcountbyStatus (countstatusfilterParams) {
      console.log(countstatusfilterParams)
      return mPool.collection('application-filters').aggregate([
        {

          $match: {

            'StatusName': {$in: ['Rejected', 'Expired']}

          }},
        {
          $group: {
            _id: '$StatusName', COUNT: {$sum: 1}
          }},
        {
          $project: {
            StatusName: 1,
            Count: '$COUNT'
          }
        },
        {
          $sort: {
            Count: -1
          }
        }
]).toArray(function (err, data) {
  if (!err) {
    console.log(data)
  }
})
    }
  }
}
like image 185
Burdy Avatar answered Sep 18 '25 10:09

Burdy