mongodb
data_error
ai_generated
true
MongoServerError: BSONObjectTooLarge: object to insert exceeds maximum BSON document size (16793600 > 16777216)
ID: mongodb/document-too-large
88%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 7 | active | — | — | — |
Root Cause
Document exceeds MongoDB's 16MB BSON size limit. Usually caused by unbounded arrays growing over time.
genericWorkarounds
-
92% success Refactor unbounded arrays into separate collection with references
Instead of: { comments: [...10000] }, use comments collection: { postId: ObjectId, text: '...' }Sources: https://www.mongodb.com/docs/manual/administration/
-
90% success Use GridFS for large binary data
const bucket = new GridFSBucket(db); bucket.openUploadStream('largefile.bin') # chunks data across documents -
85% success Use the bucket pattern to limit array growth
Split into buckets: { postId: 1, bucket: 0, comments: [...200] }, { postId: 1, bucket: 1, comments: [...200] }
Dead Ends
Common approaches that don't work:
-
Increase the BSON document size limit
95% fail
The 16MB limit is a hard server-side constraint compiled into MongoDB. It cannot be changed via configuration.
-
Compress the document before insertion
82% fail
BSON size is measured after serialization. Application-level compression does not bypass the BSON size limit check.