How To Store Audio Files in MongoDB
Introduction
If you think MongoDB is just used for storing text data, you’re only scratching the surface of its capabilities. While it’s true that MongoDB is known for its fast and powerful text search, it can also be used to store other types of files such as images and even audio files. In this article, we’ll explain how to store audio files in MongoDB with GridFS.
Prerequisites
Before we start looking at the commands needed to store audio files in MongoDB, let’s review some of the prerequisites for this task. There are a few important system requirements to confirm: ‘ * You must ensure that both MongoDB and the MongoDB Java driver have been properly configured beforehand.
You must also ensure that the latest Java JDK has been properly installed and configured before proceeding.
Last but not least, you must ensure that the MongoDB service is running.
NOTE: For the purposes of this tutorial, we assume that the MongoDB version being used is 4.0 and the MongoDB Java Driver is 3.8.2.
What is GridFS?
The GridFS API is a tool that’s commonly used for storing large files in a MongoDB database. It’s useful for storing and retrieving files that exceed 16MB– the maximum allowed size for BSON documents. GridFS works by breaking up these larger files into smaller “chunks” instead of storing it as a single document. Files are stored in two collections, which are named fs.files and fs.chunks by default; however, you can change the fs to something else if you’d like. Reading or writing a file begins with the fs.files collection. A typical fs.files document is split up in the following manner:
The fs.files
collection looks something like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | { // the unique ID of the file "_id" : ObjectId("5cf72d8ccec435132f6d1be9"), // size of each chunks, the default is 256K "chunkSize" : 261120, // date of the object creation "uploadDate" : ISODate("2019-06-05T02:48:44.749Z"), // the size of the file in bytes "length" : 6122822, // result from the "filemd5" command on this particular file chunk "md5" : "d8689e234436b6438e24cb65527e9f6d", // location of the file within the system "filename" : "/home/username/location/someAudio.mp3" } |
The fs.chunks
collection can be rather lengthy, so for this example only a portion of the results will be displayed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | { // the object id of the chunk within the _chunks collection "_id" : ObjectId("5cf72d8ccec435132f6d1bfd"), // _id of the files collection "files_id" : ObjectId("5cf72d8ccec435132f6d1be9"), // as mentioned earlier files were made up of multiple chunks, in this case // it is the 19th chunk out of 20 chunks. "n" : 19, // the payload of the chunk as a BSON binary type "data" : BinData(0,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rsMmdQidTdRniwMO58YsLGxJCmRDhnpKZLPmFHyi4EEHczTTPE1Zpk6jS") } |
How to Add files to MongoDB using GridFS
Let’s look at some commands that can be used to add audio files to a MongoDB collection. MongoDB provides a command-line utility called mongofiles
that allows users to manipulate files stored in MongoDB as GridFS
objects.
A typical usage of mongofiles
will look like the following:
1 | mongofiles <options> <commands> <filename> |
In our examples, the option that will be used is --db
, which specifies the database name on which to run mongofiles
.
The command that will be used is the command put <filename>
; this will copy the specified local file into GridFS
storage.
The code shown below can be used to add audio files in MongoDB :
1 | mongofiles --db audioDatabase put /home/username/location/someAudio.mp3 |
Your results should look something like this:
1 2 | 2019-06-05T11:24:59.306+0800 connected to: localhost 2019-06-05T11:29:56.686+0800 added file: /home/username/location/someAudio.mp3 |
You can verify that the file was added to the fs.files
collections using the commands shown below:
1 2 | // selects the database use audioDatabase |
1 2 | // display the fs.files collections db.fs.files.find().pretty() |
As a result, you should see the following:
1 2 3 4 5 6 7 | { "_id" : ObjectId("5cf72d8ccec435132f6d1be9"), "chunkSize" : 261120, "uploadDate" : ISODate("2019-06-05T02:48:44.749Z"), "length" : 6122822, "filename" : "/home/username/location/someAudio.mp3" } |
Conclusion
Not only can MongoDB help you store and manage your text data, but it can also be used to store large binary files, including audio files. The mongofiles
utility makes it easy to add audio files to a MongoDB collection– all it takes is a single command to get the job done. With the step-by-step instructions provided in this MongoDB article, you’ll be ready to add any of your audio files to a MongoDB collection.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started