mongodb data_error ai_generated true

MongoServerError: BSONObjectTooLarge: object to insert exceeds maximum BSON document size (16793600 > 16777216)

ID: mongodb/document-too-large

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7 active

Root Cause

Document exceeds MongoDB's 16MB BSON size limit. Usually caused by unbounded arrays growing over time.

generic

Workarounds

  1. 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/

  2. 90% success Use GridFS for large binary data
    const bucket = new GridFSBucket(db); bucket.openUploadStream('largefile.bin')  # chunks data across documents
  3. 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:

  1. 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.

  2. Compress the document before insertion 82% fail

    BSON size is measured after serialization. Application-level compression does not bypass the BSON size limit check.