GridFS pipe readable stream and return _id of streamed document

122 Views Asked by At

I am trying to get back the _id of a document that has been streamed to MongoDB via GridFS. Is there a way to do this?

The code is very simple. I am running the following to insert the document into MongoDB:

readableStream.pipe(bucket.openUploadStream('myFile.pdf');

Looking for a way to get the _id back from this stream - if anyone knows a way to do this please advise.

1

There are 1 best solutions below

0
hungtran273 On

We can listen to the finish event of bucket upload stream

const uploadStream = bucket.openUploadStream('myFile.pdf');

uploadStream.on("finish", (file) => {
  console.log(file); // data here
});

readableStream.pipe(uploadStream);