How can I specify a GridFS bucket?

北慕城南 提交于 2019-12-06 03:20:19

This is not well documented in gridfs-stream or the underlying native mongodb driver it uses, but here is how you do it:

Here is the options object from the gridfs-stream createWriteStream example (note the root option):

{
   _id: '50e03d29edfdc00d34000001', 
   filename: 'my_file.txt',         
   mode: 'w', 
   chunkSize: 1024, 
   content_type: 'plain/text', 
   root: 'my_collection',  // Bucket will be 'my_collection' instead of 'fs'
   metadata: {
       ...
   }
}

Why it works:

gridfs-stream passes through the options object you pass a call to createWriteStream or createReadStream to the underlying mongodb driver to create a gridStore object to represent the file. The mongodb driver in turn recognizes root in the options object as an override of the default "fs" grid bucket prefix string.

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