MongoServerError:BSONObj 大小:17825792 (0x1100000) 无效。大小必须在 0 到 16793600(16MB) 之间。第一个元素
MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) First element
ID: database/mongodb-bson-size-exceeded
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
根因分析
BSON 文档超过 MongoDB 的 16MB 文档大小限制,通常是由于插入或更新包含大型数组、嵌套数据或二进制字段(如 GridFS 块)的文档,超出最大允许大小。
English
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.
官方文档
https://www.mongodb.com/docs/manual/reference/limits/#mongodb-limit-BSON-Document-Size解决方案
-
使用 GridFS 存储大文件:将大型数据作为 GridFS 文件存储,而不是嵌入到文档中。例如:mongofiles put large_file.bin --db mydb
-
通过删除不必要的字段、将数组拆分为单独的集合或使用引用(例如,将大型数组存储在子集合中并通过 _id 链接)来减小文档大小。
无效尝试
常见但无效的做法:
-
Increasing the BSON document size limit via a configuration parameter like 'maxBsonObjectSize'
100% 失败
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% 失败
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.