Updated March 8, 2023
Introduction to Mongodb max document size
Mongodb max document size is 16 MB, we can store more than 16 MB records in one document by using GridFS, its store large documents in multiple chunks. GridFS is stored multiple files as per requirement, this is used to recall the particular section of the file without reading all the data from the memory. If suppose our document size is more than 16 MB then we are using GridFS, it will store the documents in two collections first contains the chunks and the second contains the metadata. While inserting documents into the collection we have enclosed the document with braces.
Syntax:
Below is the syntax of MongoDB max document size.
- Insert a document into the collection –
db.name_of_collection.insert ( { Field1: value1, Field2: value2, Field3: value3, …., FieldN: valueN } )
- Divide the max size documents into chunks using GridFS –
Collection 1 –
{
“id_field”: value,
“collection2_field_id”: value.
“data_of_chunks”: value,
….
“fieldN”: value
}
Collection 2 –
{
“id_field”: value,
“size_of_chunk”: value.
“name_of_file”: value,
“type_of_content”: value
}
- Find the max size documents and its size from collection –
db.name_of_collection.find (). foreach (collection_document => {
var document_size = Object.bsonsize(document object);
if (document_size > max) {
max = document_size;
id = document_id;
}
});
Parameter description syntax of max document size in MongoDB
- Name of the collection – This is defined as the name of the collection which was used to insert documents.
- Insert – This is a method used in MongoDB to insert documents into the collection. We can insert single as well as multiple documents in a single command.
- Document field – This is defined as the name of the document field which was used to insert a value into the collection.
- Document field value – This is the value of the document, we can insert any value into the document as per the data type which was we have defined for the document field.
- Chunk size – This is the size of each chunk in bytes. GridFS will divide the documents into chunk sizes. The default size of each chunk is 255 KB.
- Filename – This is an optional parameter used while doing GridFS on the document collection. This filename is in a human-readable format.
- Chunks data – This is defined as chunks payload type as BSON in binary format.
- Content-type – This is an optional parameter while using GridFS on collection documents in MongoDB. This is used for MIME type GridFS file only.
How max document size works in Mongodb?
- In MongoDB we have to store all the documents in BSON format, this is the format of old JSON.
- In MongoDB, we can store only 16 MB documents in the collection. Which was very small when we have stored large files documents such as audio or video. GridFS is very useful for large documents which exceeded the size limit is 16 MB.
- To store this type of document in a single file we have storing this document in multiple files or multiple chunks using GridFS.
- Each chunk will store in separate documents, so the size of each document is 255 KB.
- The default size of GridFS chunks in MongoDB is 255 KB, as per the order GridFS will divide our documents into the number of chunks file.
- MongoDB GridFS is using two collections to store this type of documents. The first collection contains the chunks of files and the second collection will store the metadata of the file.
- When we have executed any query in the GridFS file, the GridFS driver will reassemble the file as per need.
- Using GridFS we can access the information from the files also we can skip and resume our audio or video using GridFS.
- Also, we can perform the operations on ranged queries files that were stored by GridFS in MongoDB.
- Using GridFS we can store multiple files as per our need. Using GridFS we will recall a specific section of the file without accessing the whole file into the memory.
- GridFS will sync our files and metadata automatically and it will be deployed among the systems whenever required.
- GridFS will automatically create the index for the files to faster access the data.
- Basically, the MongoDB GridFS file system is used to store the files, but this file’s data stores in the MongoDB collection.
- We can also store the PDF files using GridFS in MongoDB. We can store large no of PDF files in MongoDB collection by using GridFS.
Example
The below example shows max document size in MongoDB.
Insert the max size of the document into the MongoDB collection –
- In the below example, we have to insert the max size of the document into the MongoDB collection. We have to give the path to download student cards from the web.
- We have inserted the documents into the collection name as max_collection.
Code –
db.max_collection.insert ( { stud_name: "ABC", stud_id: 101, grades: {first: "A", second: "B", third: "C"}, stud_marks: [50, 60, 70, 80, 90], stud_card: "https://max_collection/stud_card/downloads"} )
Figure – Example to insert the max size of the document into the MongoDB collection.
Find the max size of documents from collection –
- The below example shows find the max size of documents from the collection. We have finding max-size documents from the collection.
- In below example we can see that “60f9540fca7b9e698ed477c1” ID have max value.
Code –
var max (Its used to find max size of documents) = 0, id = null; // define value of ID field as null.
db.max_collection.find ().forEach (col_doc => {
var document_size = Object.bsonsize (col_doc);
if(document_size > max) { // If loop used to find max size of document.
max = document_size ;
id = col_doc._id;
}
});
print(""+id+ (Print the max ID field)", "+max);
db.max_collection.find ()
Figure – Example to find max size of documents from the collection.
Divide the max size of documents into chunks using GridFS
- The below example shows the divide the max size of documents into chunks using GridFS. We have to create two collections for storing the document. First collection name col1 and second collection name as col2.
Code –
db.col1.insert ( {
"name_of_file": "test.txt",
"size_of_chunk": NumberInt(21120),
"length_of_chunk": NumberInt(574)
} )
db.col1.find ()
db.col2.insert ( {
"col1_id": ObjectId("60f9618eca7b9e698ed477c3"),
"n": NumberInt(0),
"col_data": "Collection document"
} )
db.col2.find ()
Figure – Example to divide the max size of documents into chunks using GridFS.
Conclusion
Max size of documents in MongoDB is 16 MB. If we want to store documents that are larger in size than 16 MB then we have to use GridFS in MongoDB. GridFS is dividing single documents into a number of chunks file, each chunk size is 256 KB.
Recommended Articles
This is a guide to Mongodb max document size. Here we discuss How max document size works in Mongodb along with the examples. You may also have a look at the following articles to learn more –