Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What is the maximum size of a document in MongoDB?
The maximum size of a document in MongoDB is 16 MB (16,777,216 bytes). This limit ensures efficient memory usage and prevents documents from consuming excessive resources during operations.
Syntax
// Document structure with size limit
{
field1: "value1",
field2: "value2",
// ... additional fields
// Total document size ? 16 MB
}
Sample Data
Let us create a collection with sample documents ?
db.demo748.insertMany([
{_id: 101, Name: "Chris", Age: 21},
{_id: 102, Name: "Bob", Age: 20},
{_id: 103, Name: "David", Age: 23},
{_id: 104, Name: "Sam", Age: 19}
]);
{
"acknowledged": true,
"insertedIds": [101, 102, 103, 104]
}
Example
Display all documents from the collection ?
db.demo748.find();
{ "_id": 101, "Name": "Chris", "Age": 21 }
{ "_id": 102, "Name": "Bob", "Age": 20 }
{ "_id": 103, "Name": "David", "Age": 23 }
{ "_id": 104, "Name": "Sam", "Age": 19 }
Key Points
- The 16 MB limit applies to individual documents, not collections
- Documents exceeding this limit will cause an insertion error
- Large files should be stored using GridFS for files over 16 MB
- Embedded arrays and subdocuments count toward the total document size
Conclusion
MongoDB enforces a 16 MB maximum document size to maintain performance and memory efficiency. For larger data storage needs, use GridFS or consider restructuring your document design.
Advertisements
