在Mongoose中使用精简+光标和查找查询

时间:2016-08-05 12:47:55

标签: node.js mongoose

我想找到使用lean()和cursor在mongoose中使用流的好方法。 目前这是我的代码,它从mongo获取真正创建新用户数组的用户

try {
    const userList = User.find().lean().cursor();
    let users = [];

    userList.on('data', (user) => users.push(user));
    userList.on('error', () => {});
    userList.on('end', () => res.status(200).send(users));
  } catch (error) {
    console.log(error);
  }

我发现这个解决方案有点奇怪,也许有更好的实现。

1 个答案:

答案 0 :(得分:-1)

查看http://highlandjs.org/

对于高地,代码看起来像这样(未经测试):

var _h = require('highland');

_h(User.find().lean().cursor())
.toArray(function(users) {
  res.status(200).send(users)
});
相关问题