javascript - 类型错误 : Cannot call method 'toArray' of undefined while aggregatein mongo in node. js

标签 javascript node.js mongodb aggregation-framework

在 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/

相关文章:

node.js - 如何在nodejs中用swig输出JSON

javascript - 如何在 SAILS JS 的 API REST 中使用方法过滤器搜索?

mongodb - loopback mongodb 身份验证失败

reactjs - 在 react 中保存 image._id

node.js - MongoDB的cursor.each方法返回最终的空值

javascript - 从png图像创建jpg图像时将html Canvas 黑色背景更改为白色背景

JavaScript 计算不起作用

javascript - 从表行中获取所有数据任务 ID

javascript - 2 个 bootstrap.css 文件之间存在冲突

node.js - 为什么使用数据加载器进行批处理在测试中不起作用?