TechAE Blogs - Explore now for new leading-edge technologies

TechAE Blogs - a global platform designed to promote the latest technologies like artificial intelligence, big data analytics, and blockchain.

Full width home advertisement

Post Page Advertisement [Top]

GridFS MongoDB


What is GridFS?

MongoDB has been recognized as the world’s most popular NoSQL database system in 2014. What’s special about MongoDB is its ability to store large data files in GridFS, without worrying about file size limitations. GridFS is a MongoDB file system that splits large files into smaller chunks which are stored as separate documents within MongoDB collections.

GridFS Upload


Salient Features of GridFS:

✔️ Files larger than the BSON-document size limit of 16 MB are stored and retrieved.

✔️ you can store as many files as needed.

✔️ you can access information of large files without reading the entire file into memory

GridFS stores files in two collections:

☑️ fs.chunks stores the binary chunks. Following is a sample document:


{
  "_id" : <ObjectId>,
  "files_id" : <ObjectId>,
  "n" : <num>,
  "data" : <binary>
}

n is the sequence number of chunks, starting with 0.

☑️ fs.files store the file's metadata.


{
  "_id" : <ObjectId>,
  "length" : <num>,
  "chunkSize" : <num>,
  "uploadDate" : <date>,
  "filename" : <string>,
  "metadata" : <any>,
}

length is the size of the document and chunkSize is the size of each chunk in bytes.

✔️ GridFS uses an index on the files collection using the filename and uploadDate fields.

✔️ By default, GridFS uses a default chunk size of 255 kB.

How does GridFS prevent the same filename?

GridFS uses a unique, compound index on the chunks collection using the files_id and n fields allowing efficient retrieval of chunks. So if your filename is the same in any case, it will be saved by a unique "_id".

How to use GridFS?

Now, let's try using GridFS, you can use the following commands to store your large files. Open command prompt to run these:

mongofiles -d=records list # To list records collection
mongofiles -d=records put filename.ext # To put file using GridFS
mongofiles -d=records delete filename.ext # To delete file from GridFS

Let’s Put It All Together:

We started out by defining GridFS, explaining that it provides efficient features for large files.

Then, we proceeded to check out the best features of GridFS. At last, we now know how to use GridFS. Feel free to ask questions if you need any help.

Good Luck!

No comments:

Post a Comment

Bottom Ad [Post Page]