2017-08-01 111 views
0

我Mongodb聚合查詢在RoboMongo shell中正常工作,並且我正確的結果。Mongodb Nodejs驅動程序聚合查詢不返回數據

機器人蒙戈殼牌查詢

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

     $match: { 


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


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

我已經複製並粘貼相同的查詢,並試圖執行與MongoDB中的NodeJS 2.2驅動程序。這讓我沒有結果

下面是JavaScript代碼

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) 
    } 
}) 
    } 
    } 
} 

任何幫助將得到高度讚賞。

感謝

回答

0

你有沒有嘗試過這樣的:

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

因此,在這種情況下,你的MongoDB aggreage應該是:

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) 
    } 
}) 
    } 
    } 
} 
+0

不,它仍然無法正常工作。我仍然變得空白數組。 – MAQ

+0

我甚至試圖複製基於這個例子的代碼http://mongodb.github.io/node-mongodb-native/2.2/tutorials/aggregation/但仍然不起作用 – MAQ

相關問題