title: How to pipe large file into mongodb using gridfsbucket, when receiving file in chunks?
body: <p>I am using nodejs, mongodb, and gridfsbucket. </p>
  
  <p>I am receiving a file into my server in 255 byte chunks, the files can be extremely large so creating a variable to store the chunks and then piping that into mongo using gridfsbucket is not a viable option. </p>
  
  <p>Currently I have a working method of temporarily storing the file on disk and then piping that into mongo. This actually works pretty well, the only problem is I don't want to temporarily store the data before streaming into mongo using gridfsbucket.</p>
  
  <p>Does anyone know how to take those chunks as they are coming into my server and immediately stream them into mongo using gridfsbucket? I am thinking that I would need to open the pipe and then constantly stream the chunks into the pipe, but I am not sure how to accomplish this.</p>
  
  <p>This is my current code for storing to the disk:</p>
  
  <pre><code>fs.appendFileSync(this.tempFileName, Buffer.from(this.currfile.currdatachunk));
  this.currfile.filename = this.currfile.filename.replace(")", Date.now() + ")");
  var fileName = decodeURI(this.currfile.filename.replace("$(", "").replace(")", ""));
   fileName = encodeURI(fileName);
   var self = this;
   var gb = new GridFSBucket(mongoCacheDb, { bucketName: this.cacheCollection });
   var uploadStream = gb.openUploadStream(fileName);
  
   uploadStream.options.metadata = {
       'url': "/getFile/" + fileName,
       'id': uploadStream.id
   }
  
   uploadStream.once("finish", function uploadStream_onceFinish() {
                          if (this.length &gt; 0) {
                              var ms = new Message();
                              ms.data = self.cacheCollection + "/" + self.currfile.filename;
                              ms.datatype = "URL";
                              ms.hasdata = "yes";
                              ms.haserrors = "no";
                              ms.type = "APPXLOADURL";
                              sendMessage(ws, ms);
  
                              /*Send response to server indicating file receipt*/
                              var nameAB = Buffer.from(self.currfile.filename);
                              self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                              self.clientsocket.write(Buffer.from(nameAB));
                              self.clientsocket.write(Buffer.from([3, 1]));
                              console.log("Finished: " + Date.now());
                          } else {
                              var nameAB = Buffer.from(self.currfile.filename);
                              self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                              self.clientsocket.write(Buffer.from(nameAB));
                              self.clientsocket.write(Buffer.from([0, 0]));
                          }
                          fs.unlinkSync(self.tempFileName);
                      });
                      fs.createReadStream(this.tempFileName).pipe(uploadStream);
  </code></pre>