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
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)
}
})
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With