Questions about mongoc_gridfs_t

12 views
Skip to first unread message

Ingrid Santana

unread,
May 4, 2018, 7:25:44 PM5/4/18
to mongodb-user
I am following the tutorial on this link regarding the use of mongoc_gridfs_t. It has a read part, list part and write part. But, I have some questions.
  1. The read part doesn't seem to be doing anything that I can see. It runs, but I don't understand where I can see the results. Where can I read the data that was read from the database? 
  2. When the data is stored on the database, it is converted to another format. Because of that, if I try to read using mongo, I can't actually read what is inside the chunks because there is nothing understandable. There is something I can do so the information is inserted without convertion? Is the reading part supposed to undo the conversion when trying to read?

Wan Bachtiar

unread,
May 16, 2018, 3:07:03 AM5/16/18
to mongodb-user

The read part doesn’t seem to be doing anything that I can see. It runs, but I don’t understand where I can see the results. Where can I read the data that was read from the database?

Hi Ingrid,

The example GridFS executable has three modes read, write, and list.
Using the example executable you can write (upload) a file to MongoDB GridFS as below:

> ./example_gridfs write filename_a ./photo_123.png

Where filename_a is the filename that would be in GridFS, and photo_123.png is the file that you would like to upload.
Once uploaded, you can check by listing the available files, for example:

./example_gridfs list 
filename_a
filename_b

You can then read back (download) the file by:

./example_gridfs read filename_a > ./copy_of_photo_123.png

Where filename_a is the file that you would like to read/download, and copy_of_photo_123.png is the name of the target file name. Please note that the example GridFS executable dumps the content to STDOUT. This is why the example above redirect the STDOUT to a file.

Depending on what you’re aiming to do, you may also be interested in mongofiles. The mongofiles utility makes it possible to manipulate files stored in your MongoDB instance in GridFS objects from the command line.

There is something I can do so the information is inserted without convertion?

GridFS is a convention on how to store files in MongoDB greater than the 16MB BSON size limitation. There are two collections that are essentially the back-end of GridFS, the fs.files and the fs.chunks collections. The fs.chunks contains the binary representation of the files that were uploaded (unreadable). The fs.files however, contains metadata information that would be useful to find the chunks, for example: filename, md5, uploadDate, etc.

Is the reading part supposed to undo the conversion when trying to read?

Yes, the reading is supposed to combine (if required) the chunks and streamed back into the file that was uploaded.

Regards,
Wan.

Reply all
Reply to author
Forward
0 new messages