Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Storing Large Files with GridFS in MongoDB

Introduction

GridFS is a specification for storing and retrieving large files such as images, audio files, and videos in MongoDB. Instead of storing a large file in a single document, GridFS divides the file into smaller chunks and stores each chunk as a separate document. This allows for efficient storage and retrieval of large files.

Setting Up GridFS

To use GridFS, you need to have the MongoDB driver installed for your programming language. Here, we will demonstrate using Python with the PyMongo library.

Installing PyMongo

pip install pymongo

Uploading a File to GridFS

Use the following code to upload a file to GridFS:

Example: Uploading a File

import gridfs
from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017")
db = client.test
fs = gridfs.GridFS(db)

filename = "example.txt"
with open(filename, "rb") as f:
    file_id = fs.put(f, filename=filename)

print(f"Uploaded file with ID: {file_id}")
            

Downloading a File from GridFS

Use the following code to download a file from GridFS:

Example: Downloading a File

file_id = ... # The ID of the file you want to download
output_filename = "downloaded_example.txt"

with open(output_filename, "wb") as f:
    f.write(fs.get(file_id).read())

print(f"Downloaded file to: {output_filename}")
            

Listing Files in GridFS

Use the following code to list all files stored in GridFS:

Example: Listing Files

for file in fs.find():
    print(f"Filename: {file.filename}, ID: {file._id}")
            

Deleting a File from GridFS

Use the following code to delete a file from GridFS:

Example: Deleting a File

file_id = ... # The ID of the file you want to delete
fs.delete(file_id)
print(f"Deleted file with ID: {file_id}")
            

Conclusion

In this tutorial, you have learned how to use GridFS in MongoDB to store and retrieve large files. GridFS provides an efficient way to handle large files, making it suitable for applications that need to store and manage multimedia content.