Stream MongoDB results to Express response

浪子不回头ぞ 提交于 2020-01-16 06:05:25

问题


I am trying to do this without and 3rd party dependencies, as I don't feel they should be needed. Please note, due to architect ruling we have to use MongoDB native, and not Mongoose (don't ask!).

Basically I have a getAll function, that will return all documents (based on passed in query) from a single collection.

The number of documents, could easily hit multiple thousand, and thus, I want to stream them out as I receive them.

I have the following code:

db.collection('documents')
    .find(query)
    .stream({
        transform: (result) => {
            return JSON.stringify(new Document(result));
        }
    })
    .pipe(res);

Which kind of works, except it destroys the array that the documents should sit in, and it responds {...}{...}

There has to be a way of doing this right?


回答1:


What you can do is to write explicitly the start of the array res.write("[") before requesting the database, put a ,, on every json stringified object and on the stream end write the end of the array res.write("]") this can work. But it is not advisable!

JSON.stringify is a very slow operation, you should try to use it as less as possible.

A better approach will be to go with a streamable JSON.stringify implementation like json-stream-stringify

const JsonStreamStringify = require('json-stream-stringify');
app.get('/api/users', (req, res, next) => {
   const stream = db.collection('documents').find().stream();
   new JsonStreamStringify(stream).pipe(res);
);

Be aware of using pipe in production, pipe does not destroy the source or destination stream when errors. It is advisable to go for pump or pipeline in production to avoid memory leaks.



来源:https://stackoverflow.com/questions/53849735/stream-mongodb-results-to-express-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!