最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose.js: What are QueryStreams - Stack Overflow

programmeradmin0浏览0评论

I looked through the documentation of mongoosejs odm and found following: .html

What are they used for? What can I do with them.

I am not sure if they are used for streaming docs or for dynamicly updating queries...

Regards

I looked through the documentation of mongoosejs odm and found following: http://mongoosejs./docs/querystream.html

What are they used for? What can I do with them.

I am not sure if they are used for streaming docs or for dynamicly updating queries...

Regards

Share Improve this question asked Jul 19, 2012 at 15:04 bodokaiserbodokaiser 15.8k27 gold badges100 silver badges143 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Well, it's all about the API.

QueryStream allows to to use ReadStream's API so in order to appreciate QueryStream, you need to know more about ReadStream/WriteStream.

There are many pros:

  • You can process large amount of data, which you'll be getting as "chunks" so the memory contains one item at a time (it could of a DB document, DB row, a single line from the file, etc.)
  • You can pause/resume the stream(s)
  • You can pipe read->write very easily

The idea is that it gives you a unified API for read and write operations.

To answer your question "What can I do with them":

You could do anything with or without node.js's stream API but it definitely makes it clearer and easier to use when there's some sort of standard.

Also, node.js's streams are event-based (based on EventEmitter) so it helps with decoupling.


Edit:

That was more about the aspect of streams. In Mongoose's case, a single chunk contains a document.

To clarify the advantage of the API:

node.js's http.ServerResponse is a writable-stream, which means you should be able to stream Mongoose's resultset to the browser using a single line:

// 'res' is the http response from your route's callback.
Posts.find().stream().pipe(res); 

The point is that it doesn't matter if you're writing to http.ServerResponse, a file or anything else. As long as it implements a writable stream, it should work without changes.


Hope I made it clearer.

发布评论

评论列表(0)

  1. 暂无评论