Error
Error Code: 10334

MongoDB Error 10334: BSON Object Too Large

📦 MongoDB
📋

Description

This error indicates that a BSON document you are trying to insert or update exceeds MongoDB's maximum document size limit. MongoDB enforces a strict 16MB limit for individual BSON documents to ensure efficient memory usage and predictable performance.
💬

Error Message

B S O N Object Too Large
🔍

Known Causes

4 known causes
⚠️
Exceeding BSON Document Size Limit
Each BSON document in MongoDB has a strict maximum size limit of 16 megabytes. This error occurs when an operation attempts to create or modify a document that surpasses this limit.
⚠️
Storing Large Binary Data Directly
Attempting to embed large files, such as images, videos, or extensive binary data, directly within a document can quickly lead to the document exceeding the 16MB BSON limit.
⚠️
Embedding Large Arrays or Sub-documents
Documents containing very large arrays or deeply nested sub-documents, especially with many entries or extensive content, can grow excessively and breach the size constraint.
⚠️
Inefficient Schema Design
Poorly designed schemas that encourage embedding too much related data directly within a single document can lead to documents growing beyond the BSON size limit over time.
🛠️

Solutions

3 solutions available

1. Reduce Document Size Through Data Modeling medium

Optimize your data structure to avoid excessively large BSON documents.

1
Analyze your data model to identify fields contributing to large document sizes. This often includes deeply nested arrays or large embedded documents.
2
Consider denormalization strategies. If an array within a document is growing too large, it might be a candidate for being moved to a separate collection and linked via an ObjectId.
3
If you are embedding frequently updated or very large sub-documents, consider if they can be stored separately and referenced.
4
For large binary data (e.g., images, videos), use GridFS instead of embedding them directly within documents.

2. Modify Application Logic to Handle Large Data medium

Adjust your application code to process and insert data in smaller chunks.

1
If your application is generating very large documents, review the code that constructs these documents. Look for opportunities to break down large data sets into multiple smaller documents.
2
When inserting data, use batch operations to insert multiple smaller documents instead of attempting to insert a single, massive document. This is particularly relevant for bulk data loading.
db.collection.insertMany([{ ... }, { ... }, ...])
3
If you are updating a document that is approaching the BSON size limit, consider splitting the update into multiple smaller operations or restructuring the data being updated.

3. Increase MongoDB's `maxBsonObjectSize` Limit (Use with Caution) advanced

Temporarily increase the maximum allowed BSON object size.

1
Locate your MongoDB configuration file (e.g., `mongod.conf`). This file's location varies by operating system and installation method.
2
Add or modify the `maxBsonObjectSize` parameter within the `storage.wiredTiger` section (for WiredTiger storage engine, which is default) or at the top level for older engines. The default is 16MB (16777216 bytes). You can increase this value, but be mindful of system memory and performance.
storage:
  wiredTiger:
    engineConfig:
      maxBsonObjectSize: 33554432 # Example: doubling to 32MB
3
Restart the MongoDB server for the configuration change to take effect.
sudo systemctl restart mongod
4
Monitor your system's memory usage and overall performance after increasing this limit. Excessive values can lead to performance degradation and out-of-memory errors.
🔗

Related Errors

5 related errors