# MongoServerError：BSONObj 大小：17825792 (0x1100000) 无效。大小必须在 0 到 16793600(16MB) 之间。第一个元素

- **ID:** `database/mongodb-bson-size-exceeded`
- **领域:** database
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

BSON 文档超过 MongoDB 的 16MB 文档大小限制，通常是由于插入或更新包含大型数组、嵌套数据或二进制字段（如 GridFS 块）的文档，超出最大允许大小。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |

## 解决方案

1. ```
   使用 GridFS 存储大文件：将大型数据作为 GridFS 文件存储，而不是嵌入到文档中。例如：mongofiles put large_file.bin --db mydb
   ```
2. ```
   通过删除不必要的字段、将数组拆分为单独的集合或使用引用（例如，将大型数组存储在子集合中并通过 _id 链接）来减小文档大小。
   ```

## 无效尝试

- **Increasing the BSON document size limit via a configuration parameter like 'maxBsonObjectSize'** — 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. (100% 失败率)
- **Splitting the document into multiple smaller documents but keeping the same structure without using references** — 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. (60% 失败率)
