2015-11-07 29 views
2

在node.js.中執行聚合時出現以下錯誤。TypeError:無法在node.js中調用方法'toArray'undefined while aggregatein mongo

錯誤:類型錯誤:無法調用「指定者」未定義

doctorsCollection.aggregate([ 
    {$project:{"treatments._id":1, "treatments.price":1}}, 
    {$unwind:"$treatments"}, 
    {$match:{"treatments._id": parseInt(treatments[i])}}, 
    {$sort:{"treatments.price":-1}}, 
    {$limit:1} 
]).toArray(function(err, result) { 
    console.log(err); 
}); 

我不知道發生了什麼事情。任何幫助,將不勝感激。謝謝!

+1

'.aggregate'方法在特定版本的驅動程序之前不會返回遊標,或者您未指定將遊標作爲選項指定。直接傳遞迴調以獲得結果。 –

回答

2

對於node.js MongoDB驅動程序,.aggregate documentation不會返回遊標,除非您指定它作爲選項返回遊標。默認情況下,它將返回null,所以.toArray方法不起作用/有意義。

有幾種方法來處理它,文檔提供了大量的例子。在你的情況下,你可以添加{cursor: {batchSize: 1}}作爲.aggregate的第二個參數,它會起作用。

相關問題