4

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

1
  • 2
    did you solve the problem? I'm facing the same problem Commented May 29, 2019 at 12:45

1 Answer 1

2

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)
  }
})
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

No it still doesn't work. I'm still getting blank array.
I even tried to replicate the code based on this example mongodb.github.io/node-mongodb-native/2.2/tutorials/aggregation But still doesn't works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.