EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB GridFS
Secondary Sidebar
MongoDB Tutorial
  • Advanced
    • MongoDB Array
    • PostgreSQL ARRAY_AGG()
    • Indexes in MongoDB
    • MongoDB create Index
    • MongoDB Collection
    • MongoDB List Collections
    • MongoDB Capped Collections
    • MongoDB Delete Collection
    • Mongodb show collections
    • MongoDB Auto Increment
    • MongoDB Triggers
    • MongoDB Projection
    • Replication in MongoDB
    • MongoDB Database
    • Mongo DB Create Database
    • MongoDB Compass
    • MongoDB Users
    • MongoDB Authentication
    • MongoDB GridFS
    • MongoDB Relationships
    • MongoDB MapReduce
    • MongoDB Geospatial
    • MongoDB Monitoring
    • Backup in MongoDB
    • MongoDB Sharding
    • MongoDB Java Drivers
    • MongoDB Import
    • Mongo Database Interview Questions
    • MongoDB Join Two Collections
    • MongoDB Group by Multiple Fields
    • MongoDB Pagination
    • MongoDB Replica Set
    • MongoDB Bulk Update
    • MongoDB greater than
    • MongoDB Encryption
    • MongoDB find in array
    • MongoDB like query
    • Mongodb shell
    • MongoDB port
    • MongoDB Query Operators
    • MongoDB Web Interface
    • MongoDB Query Array
    • MongoDB Transactions
    • MongoDB Not In
    • MongoDB not null
    • MongoDB npm
    • MongoDB Remove
  • Basics
    • What is MongoDB
    • How To Install MongoDB
    • MongoDB Tools
    • MongoDB GUI Tools
    • MongoDB Versions
    • MongoDB Commands
    • Advantages of MongoDB
    • MongoDB Features
    • Is MongoDB NoSQL
    • Is MongoDB Open Source
    • Build Web Applications using MongoDB
    • MongoDB Data Types
    • MongoDB Administration
    • Data Modeling in MongoDB
    • MongoDB vs Elasticsearch
    • MariaDB vs MongoDB
    • Firebase vs MongoDB
  • Commands
    • Mongodb updateMany
    • MongoDB Aggregation
    • Mongodb unwind
    • Mongodb where
    • MongoDB BSON
    • MongoDB Filter
    • Mongodb Match
    • MongoDB sort by date
    • MongoDB Limit()
    • MongoDB Atlas Login
    • MongoDB Relational Database
    • MongoDB count
    • MongoDB Aggregate
    • MongoDB Distinct
    • MongoDB Unique
    • MongoDB find
    • MongoDB findOne()
    • MongoDB insert
    • MongoDB Delete
    • MongoDB Update
    • Lookup in MongoDB
    • order by in MongoDB
    • MongoDB $regex
    • MongoDB $elemMatch
    • MongoDB ObjectId()
    • MongoDB Skip()
    • MongoDB findAndModify
    • Mongodb findOneAndUpdate
    • MongoDB Date Query
    • MongoDB Timestamp
    • MongoDB sort()
    • MongoDB group by
    • MongoDB Join

Related Courses

MongoDB Certification Course

Oracle Certification Course

All in One Data Science Course

SQL Training Course

Oracle DBA Course

MS SQL Certification Course

MongoDB GridFS

By Sulaksh MoreSulaksh More

MongoDB GridFS

Introduction to MongoDB GridFS

Working with MongoDB, we often have to tackle the need for storing large files; these files can be images, video and audio files, etc. This is where MongoDB’s GridFS, also known as Grid File System, can be used. Basically, dividing such large files into small chunks and storing each chunk in a separate document is the main purpose of GridFS. This Grid File System is largely used to store files that cannot be stored in a collection while maintaining the limit. The MongoDB’s GridFS also has limitations like the files stored in GridFS can impact your working files saved in collections. Performance can be affected due to the storage of large files.

Syntax:

The syntax is an important aspect of any executable command. Here we have to see one notable thing: the query to insert a file into gridfs is not executed through mongodb shell, but instead from a Linux, Windows or Mac command prompt.

Given below is the standard syntax for inserting a simple file in GridFS.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

mongofiles -d database-name put filename

The above syntax will insert a file into a database. The “put” does the insertion job, while database name and file names are destination and source. Along with put, there are multiple options for deleting, viewing in the form of a list and removing the files.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)

How GridFS works in MongoDB?

MongoDB has a limit for each document size that can be stored; now, the large files cannot be stored single-handedly due to file size; this is where GridFS can be used. MongoDB’s GridFS works by saving multiple numbers of chunks, of a single large file. This way, the large files can be stored and retrieved easily when needed. The usual recommended size for each separate chunk is 256kb.

Every file that exceeds the limit of 16 MB, which is for BSON document size limit, that file will be stored and retrieved using GridFS. It is also important to know that GridFS does not support Atomic Updating. So, to put it simply. GridFS is a file system, that stores data, of larger size, but in mongodb collections and the form of chunks.

Examples of MongoDB GridFS

Given below are the examples mentioned:

Example #1

We will experiment with our very first example with a simple audio file. Right now, we don’t have any specific collection for gridfs files, but as we know, MongoDB has an amazing feature of creating new collections when we insert a document into the non-existing collection.

Following is the query for inserting a file into gridfs, unlike other insert operations where we insert files through Mongo Shell, here we will insert to Machine Terminal.

Code:

/usr/bin$ mongofiles -d gridfs put /home/sulaksh/Desktop/potkesd/c.mp3

The above query is a standard format for inserting files into gridfs. We have navigated the terminal to the location where mongofiles, an executable file, are saved. Our query begins with the keyword, mongofiles, followed by the Storage Options code, “-d”. This -d specifies the database where the file is to be stored. This -d option is followed by the database name, here our database is named as gridfs. At the moment, we don’t have any database with such a name, but it’ll be created.

Output:

MongoDB GridFS 1

Now, how to retrieve the data that we saved using mongofiles is simply by executing the find query.

Code:

db.fs.files.find().pretty()

The above query will return all documents stored in the fs.files collection, and adding pretty() makes it represent the data in JSON format.

Output:

simply return all documents stored in the fs.files

As you can see in the above screenshot, we have our filename, chunk size, and other insertion operation details.

Example #2

Upon insertion, we understood how the operation to insert a file in GridFS works, let us now delete the same file and see how Deletion works.

Code:

/usr/bin$ mongofiles -d gridfs delete /home/sulaksh/Desktop/potkesd/c.mp3

The major difference between the put and delete query is the delete keyword itself.

Output:

MongoDB GridFS 3

Example #3

Insert and Delete are important operation, just like Search. For our next example, we will implement a search option along with mongofiles to search for a file in gridfs.

Code:

/usr/bin$ mongofiles -d gridfs search Poster

Above query will search for any and all documents that store file which contains the Poster word.

Output:

implement search option

And as you can see, it returned the record, that has a file with Poster as the file name.

Example #4

We will execute a list option, which will return every file stored in the database, here it won’t return the file in chunks or parts but as a whole.

Code:

mongofiles -d gridfs list

Output:

will return every file stored in the database

Like other queries, the -d denotes the database and gridfs is the name of the database and the command list. Upon execution, all the present files stored in the gridfs database in the form of chunks are retrieved.

Using these put, list, search options, we can easily use and implement the Grid File System in real-time application.

Conclusion

To Conclude, MongoDB’s amazing feature, GridFS, allows you to store files which exceed the size limit for a single document. Here, the files are stored in small chunks and two different collections. One collection stores the actual data, the file, while the other collection stored the file’s meta data. Put, Search, List, etc. are the options provided to work with the Grid File System. With the advantage of storing large files, GridFS can result in slow performance and can impact the working data set.

Recommended Articles

This is a guide to MongoDB GridFS. Here we discuss the introduction to GridFS in MongoDB, how does it work along with respective examples. You may also have a look at the following articles to learn more –

  1. MongoDB Administration
  2. Indexes in MongoDB
  3. order by in MongoDB
  4. Lookup in MongoDB
Popular Course in this category
MongoDB Training Program (4 Courses, 2 Projects)
  4 Online Courses |  2 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle Training (14 Courses, 8+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
Oracle DBA Database Management System Training (2 Courses)4.7
MS SQL Training (16 Courses, 11+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more