首页 > 解决方案 > 聚合上的 Mongoskin 光标

问题描述

我在 nodejs 中使用 mongoskin 进行数据库查询。
我无法在聚合查询上使用游标,而在查找查询的情况下它工作正常。

    //This code is working fine
    var cursor = db.collection('users').find();
    cursor.forEach(function(user){
    console.log(user);
    }) 

    //The below query is not working
    var cursor = db.collection('users').aggregate([{$sort:{username:1}}]);
    cursor.forEach(function(user){
    console.log(user);
    }) 
    //error - cursor.forEach is not a function

有没有其他方法可以使用 mongoskin 在聚合上使用游标?
“mongoskin”:“^2.1.0”

标签: node.jsmongodbmongoskin

解决方案


我遇到过同样的问题。我为它创建了一个库。它允许使用聚合编写 foreach 并且可以使用游标运行。mongoskin 光标

示例:db.collection('collectionname').aggregate().forEach(function(){}); db.collection('collectionname').aggregateAsync().then(function(){});


推荐阅读