MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) First element
ID: database/mongodb-bson-size-exceeded
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
Root Cause
A BSON document exceeds MongoDB's 16MB document size limit, typically caused by inserting or updating a document with large arrays, nested data, or binary fields like GridFS chunks that exceed the maximum allowed size.
generic中文
BSON 文档超过 MongoDB 的 16MB 文档大小限制,通常是由于插入或更新包含大型数组、嵌套数据或二进制字段(如 GridFS 块)的文档,超出最大允许大小。
Official Documentation
https://www.mongodb.com/docs/manual/reference/limits/#mongodb-limit-BSON-Document-SizeWorkarounds
-
95% success Use GridFS for large files: store the large data as a GridFS file instead of embedding it in a document. Example: mongofiles put large_file.bin --db mydb
Use GridFS for large files: store the large data as a GridFS file instead of embedding it in a document. Example: mongofiles put large_file.bin --db mydb
-
85% success Reduce document size by removing unnecessary fields, splitting arrays into separate collections, or using references (e.g., store large arrays in a subcollection and link via _id).
Reduce document size by removing unnecessary fields, splitting arrays into separate collections, or using references (e.g., store large arrays in a subcollection and link via _id).
中文步骤
使用 GridFS 存储大文件:将大型数据作为 GridFS 文件存储,而不是嵌入到文档中。例如:mongofiles put large_file.bin --db mydb
通过删除不必要的字段、将数组拆分为单独的集合或使用引用(例如,将大型数组存储在子集合中并通过 _id 链接)来减小文档大小。
Dead Ends
Common approaches that don't work:
-
Increasing the BSON document size limit via a configuration parameter like 'maxBsonObjectSize'
100% fail
MongoDB does not allow changing the 16MB limit; it is a hard-coded limit in the server. Attempting to set such a parameter will be ignored or cause an error.
-
Splitting the document into multiple smaller documents but keeping the same structure without using references
60% fail
If the application logic expects a single document, splitting may break queries; also, if the data is inherently large (e.g., a large file), splitting into separate documents may not be appropriate without using GridFS.