sve*_*rgr 2 nonblocking blocking mongoose mongodb node.js
我如何通过mongoose获得一个大型集合,我得到的每个文件都返回,而不是整个集合的大数组?
目前我只是使用以下查询:
var query = templateData.find({});
query.exec(function (err, docs) {
// docs as array
});
Run Code Online (Sandbox Code Playgroud)
这样,查询功能就像阻塞IO而不是非阻塞.有没有办法让这更无阻塞?
好吧,因为我在发布这个问题之后再看了一下mongoose文档,我完成了这个stream()功能,完全填满了非阻塞操作.
怪我,但我认为在mongoose文档中可能会提到更多冒犯:http: //mongoosejs.com/docs/api.html#query_Query-stream
var query = templateData.find({}).stream();
query.on('data', function (doc) {
// do something with the mongoose document
}).on('error', function (err) {
// handle the error
}).on('close', function () {
// the stream is closed
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2634 次 |
| 最近记录: |