在 Node.js 中进行聚合时出现以下错误。
错误:类型错误:无法调用未定义的“toArray”方法
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);
});
我不知道发生了什么。任何帮助,将不胜感激。谢谢!
最佳答案
来自.aggregate
documentation对于node.js MongoDB 驱动程序,该方法不会返回游标,除非您指定它返回游标作为选项。默认情况下,它将返回 null
,因此 .toArray
方法不起作用/没有意义。
有多种方法可以处理它,并且文档提供了大量示例。在您的情况下,您可以将 {cursor: {batchSize: 1}}
添加为 .aggregate
的第二个参数,它将起作用。
关于javascript - 类型错误 : Cannot call method 'toArray' of undefined while aggregatein mongo in node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588479/