WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

MongoDB · Advanced · question 43 of 100

How do you manage large files in MongoDB using GridFS, and what are its benefits?

📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

MongoDB provides a feature called GridFS to store and manage large files efficiently. Instead of storing large files in a single document, GridFS divides the file into multiple parts, called chunks, and stores each chunk as a separate document. It also stores metadata about the file, such as the filename, content type, and file size, in a separate document called the files collection.

To use GridFS, you first need to create a connection to your MongoDB database and initialize a GridFS bucket. In Python, you can do this using the following code:

from pymongo import MongoClient
from gridfs import GridFS

client = MongoClient('localhost', 27017)
db = client['my_database']
fs = GridFS(db, collection='my_files')

Once you have initialized the GridFS bucket, you can use its methods to store and retrieve files. To store a file in GridFS, you can use the ‘put()‘ method, as shown below:

with open('large_file.pdf', 'rb') as f:
    file_id = fs.put(f, filename='large_file.pdf', content_type='application/pdf')
    print(file_id)

This will upload the file ‘large_file.pdf‘ to GridFS and return the unique identifier assigned to the file. The metadata about the file, including the filename and content type, is also stored in the files collection.

To retrieve a file from GridFS, you can use the ‘get()‘ method, as shown below:

import io

file_id = ObjectId('60cde9c450ef306f1bacb99a')
f = fs.get(file_id)
content = f.read()

This will retrieve the file with the specified ID from GridFS and return it as a file object, which can be read using Python’s standard I/O functions. Note that the file is retrieved in chunks, and the ‘read()‘ method returns the concatenated contents of all the chunks.

One of the major benefits of using GridFS to manage large files in MongoDB is that it allows you to store files larger than the 16 MB size limit for BSON documents. GridFS automatically divides the file into chunks and stores each chunk as a separate document, allowing you to store files of virtually any size. Additionally, because GridFS stores metadata about the file separately from the chunks themselves, it enables you to efficiently query and retrieve files based on their attributes, such as filename or content type.

Another benefit of GridFS is that it provides a standardized way to store and retrieve large files in MongoDB, making it easy to integrate with other tools and applications that rely on MongoDB for data storage. For example, you could use GridFS to store video files for a web application, and then use MongoDB’s aggregation and indexing capabilities to search and filter the files based on their metadata.

In summary, GridFS provides a powerful and flexible mechanism for managing large files in MongoDB, allowing you to store files of virtually any size and leverage MongoDB’s rich querying and indexing capabilities to efficiently search and retrieve them.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic MongoDB interview — then scores it.
📞 Practice MongoDB — free 15 min
📕 Buy this interview preparation book: 100 MongoDB questions & answers — PDF + EPUB for $5

All 100 MongoDB questions · All topics