I am trying to send a large result from a Mongo database to a Koa application user (using Mongoose).
I initially had something like:
var res = yield Model.find().limit(500).exec(); this.body = {data: res};
However, the size of the sent result set sent caused the application to time out and, as such, I would like to pass the response as it comes from the database.
With Mongoose, you can turn the result of a query into a stream by doing something like:
var stream = Model.find().limit(300).stream();
However, I am not sure how to write this stream in response, keeping the required format. I want this to happen:
this.body.write("{data: "}); this.body.write(stream); this.body.write("}");
but I know that Koa does not have body.write, and I'm sure I don't use streams either. Can someone point me in the right direction?
source share