Search code examples
node.jsmongodbmongoosegridfsgridfs-stream

How to use openUploadStream with Grid-fs bucket


I would like to use a mongodb model with the GridFSBucket.

I realized that you can use the metadata with GridFSUploadOptions, and it seems to function as the model in this case, but i get an error, "TypeError: mongodb.GridFSUploadOptions is not a constructor" when i use it

       let bucket = new mongodb.GridFSBucket(conn.db, {
            bucketName: 'tracks'
        });

        let options = new mongodb.GridFSUploadOptions();


        let uploadStream = bucket.openUploadStream(trackName, options.metadata(new Document("Speaker", "Bill Gates").append("Duration", "1hr")));

I expect to have a way to add more user information added to the file created.


Solution

  • I got it after 3 days of brain storming, gridfs stores data as .files and .chunks, i didnt know how to build a model for this, after researching i realized you could add more info to the .files stored in the db about the data. There is nothing online on how to do this just hints like this "openUploadStream(filename, options)", after several hours of research i finally got it somehow.

    let bucket = new mongodb.GridFSBucket(conn.db, {
                bucketName: 'tracks'
            });
    
            let uploadStream = bucket.openUploadStream(trackName, {chunkSizeBytes:null, metadata:{speaker: "Bill Gates", duration:"1hr"}, contentType: null, aliases: null});
    

    check the .files in the database, metadata will be filled.