Wednesday, October 12, 2022

Mongo GridFS how to add files

mongofiles.exe -d gridfs put song.mp3

Here, gridfs is the name of the database in which the file will be stored. If the database is not present, MongoDB will automatically create a new document on the fly. Song.mp3 is the name of the file uploaded. To see the file's document in database, you can use find query −

db.fs.files.find()

The above command returned the following document −

{

   _id: ObjectId('534a811bf8b4aa4d33fdf94d'), 

   filename: "song.mp3", 

   chunkSize: 261120, 

   uploadDate: new Date(1397391643474), md5: "e4f53379c909f7bed2e9d631e15c1c41",

   length: 10401959 

}

We can also see all the chunks present in fs.chunks collection related to the stored file with the following code, using the document id returned in the previous query −

db.fs.chunks.find({files_id:ObjectId('534a811bf8b4aa4d33fdf94d')})

references:

https://www.tutorialspoint.com/mongodb/mongodb_gridfs.htm#:~:text=GridFS%20is%20the%20MongoDB%20specification,document%20size%20limit%20of%2016MB.

No comments:

Post a Comment