{"id":309,"date":"2025-11-01T04:05:10","date_gmt":"2025-11-01T04:05:10","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=309"},"modified":"2025-11-05T15:49:30","modified_gmt":"2025-11-05T15:49:30","slug":"what-is-mongodb-gridfs","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=309","title":{"rendered":"what is mongodb gridfs?"},"content":{"rendered":"\n<p><strong>GridFS<\/strong> stands for <strong>Grid File System<\/strong>. <strong>MongoDB GridFS<\/strong> is a specification for storing and retrieving <strong>large files<\/strong> (such as images, videos, audio files, or any binary data) in <strong>MongoDB<\/strong> that exceed the BSON document size limit of <strong>16 MB<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p>GridFS uses <strong>two collections<\/strong> to manage files:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>fs.files<\/code><\/strong> \u2013 Stores file metadata such as filename, upload date, size, and a unique ID.<\/li>\n\n\n\n<li><strong><code>fs.chunks<\/code><\/strong> \u2013 Stores the actual file data in chunks, each linked to the parent file via an ObjectId.<\/li>\n<\/ol>\n\n\n\n<p>Each chunk document in <code>fs.chunks<\/code> looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": ObjectId(\"...\"),\n  \"files_id\": ObjectId(\"...\"),  \/\/ reference to fs.files._id\n  \"n\": 0,                       \/\/ chunk sequence number\n  \"data\": BinData(...)           \/\/ binary data\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> Example Flow<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>You upload a large file.<\/li>\n\n\n\n<li>GridFS splits it into chunks (e.g., 255 KB each).<\/li>\n\n\n\n<li>Each chunk is stored in <code>fs.chunks<\/code>.<\/li>\n\n\n\n<li>Metadata about the file (like name, length, and MD5 hash) goes into <code>fs.files<\/code>.<\/li>\n\n\n\n<li>When you download or read the file, GridFS reassembles it in order.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Tools &amp; Drivers<\/h3>\n\n\n\n<p>Most MongoDB drivers include built-in support for GridFS.<br>For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Node.js:<\/strong> <code>GridFSBucket<\/code> API in the official <code>mongodb<\/code> driver.<\/li>\n\n\n\n<li><strong>Python:<\/strong> <code>gridfs<\/code> module in <code>pymongo<\/code>.<\/li>\n\n\n\n<li><strong>Mongo Shell:<\/strong> Some tools like <code><strong>mongofiles<\/strong><\/code> let you work with GridFS directly.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How to Add (Upload) Images to GridFS Manually via Command Line<\/h2>\n\n\n\n<p>MongoDB provides a built-in tool called <strong><code>mongofiles<\/code><\/strong> for working with GridFS from the terminal.<\/p>\n\n\n\n<p>You can use it to <strong>upload, list, download, or delete files<\/strong> in GridFS \u2014 no programming needed.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> Step-by-Step Example<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. Make sure MongoDB is running<\/h4>\n\n\n\n<p>If you\u2019re running MongoDB locally:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongod\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. Open another terminal window<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">3. Use the <code>mongofiles<\/code> tool<\/h4>\n\n\n\n<p>Here\u2019s the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db &lt;database_name&gt; put &lt;filename&gt;\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> Example \u2014 Upload an Image<\/h3>\n\n\n\n<p>Suppose you have a file named <strong><code>photo.jpg<\/code><\/strong> in your current folder.<\/p>\n\n\n\n<p>Run this command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=mydatabase put photo.jpg\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--db=mydatabase<\/code> \u2192 stores file in the <code>mydatabase<\/code> database<\/li>\n\n\n\n<li><code>put photo.jpg<\/code> \u2192 uploads the file into GridFS<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> After Uploading<\/h3>\n\n\n\n<p>If you open the MongoDB shell and connect to your database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongosh\nuse mydatabase\n<\/code><\/pre>\n\n\n\n<p>Then check the collections:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>show collections\n<\/code><\/pre>\n\n\n\n<p>You\u2019ll see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fs.files\nfs.chunks\n<\/code><\/pre>\n\n\n\n<p>Now, you can inspect what\u2019s inside:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.fs.files.find().pretty()\n<\/code><\/pre>\n\n\n\n<p>You\u2019ll get something like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": ObjectId(\"654240f57b7d9e47f1fce9b2\"),\n  \"length\": 523424,\n  \"chunkSize\": 261120,\n  \"uploadDate\": ISODate(\"2025-11-01T12:00:00Z\"),\n  \"filename\": \"photo.jpg\"\n}\n<\/code><\/pre>\n\n\n\n<p>And in <code>fs.chunks<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db.fs.chunks.find().limit(2).pretty()\n<\/code><\/pre>\n\n\n\n<p>Each document looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"_id\": ObjectId(\"654240f57b7d9e47f1fce9b3\"),\n  \"files_id\": ObjectId(\"654240f57b7d9e47f1fce9b2\"),\n  \"n\": 0,\n  \"data\": BinData(0,\"\/9j\/4AAQSkZJRgABAQ...\")\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> To Download the Image Back<\/h3>\n\n\n\n<p>If you want to download the file again from GridFS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=mydatabase get photo.jpg\n<\/code><\/pre>\n\n\n\n<p>This will create a new file named <code>photo.jpg<\/code> in your current folder.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"> To Delete the Image<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=mydatabase delete photo.jpg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Command<\/th><th>Action<\/th><\/tr><\/thead><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>mongofiles --db=mydb put file.jpg<\/code><\/td><td>Upload (store in GridFS)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>mongofiles --db=mydb list<\/code><\/td><td>List all stored files<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>mongofiles --db=mydb get file.jpg<\/code><\/td><td>Download a file<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code>mongofiles --db=mydb delete file.jpg<\/code><\/td><td>Delete a file<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Full Example Workflow<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1 \u2014 Start MongoDB<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongod --dbpath \"C:\\data\\db\"\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2 \u2014 Upload a file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=test put 1.png\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3 \u2014 List files in GridFS<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=test list\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4 \u2014 Download the file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=test get 1.png\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5 \u2014 Delete it (optional)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mongofiles --db=test delete 1.png<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GridFS stands for Grid File System. MongoDB GridFS is a specification for storing and retrieving large files (such as images, videos, audio files, or any binary data) in MongoDB that exceed the BSON document size limit of 16 MB. How It Works GridFS uses two collections to manage files: Each chunk document in fs.chunks looks [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-mongodb"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309"}],"version-history":[{"count":2,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}