问题描述
我一直在寻找如何将MongoDB查询结果流式传输到nodejs客户端的示例.到目前为止,我发现的所有解决方案似乎都是立即读取查询结果,然后将结果发送回服务器.
I have been searching for an example of how I can stream the result of a MongoDB query to a nodejs client. All solutions I have found so far seem to read the query result at once and then send the result back to the server.
相反,(显然)我想为查询方法提供一个回调,并在结果集的下一个块可用时让MongoDB调用.
Instead, I would (obviously) like to supply a callback to the query method and have MongoDB call that when the next chunk of the result set is available.
我一直在看猫鼬-我可能应该使用其他驱动程序吗?
I have been looking at mongoose - should I probably use a different driver?
Jan
推荐答案
Mongoose中的流式传输在2.4.0版本中可用,该版本出现在发布问题后三个月:
Streaming in Mongoose became available in version 2.4.0 which appeared three months after you've posted this question:
Model.where('created').gte(twoWeeksAgo).stream().pipe(writeStream);
可以在其文档页面中找到详细说明的示例.
More elaborated examples can be found on their documentation page.
这篇关于如何使用Node.js串流MongoDB查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!