I'm working on a NodeJS app, and I need to create a JSON file out of a MongoDB collection. I need to perform some logic on each object, so I'm streaming a Mongo cursor through a transform pipe and then writing to a JSON file.
However, the JSON file turns to be a bunch of individual objects. I would like to wrap all documents into a single JSON object, but I can't figure out how.
I am able to get the desired result by querying the collection .toArray()
, then performing the logic on the whole array and using Object.assign({}, array)
, but as you can imagine this takes forever and does not take advantage of streams.