# MongoServerError: 事务 API 版本错误：预期 API 版本 '1' 但收到 '2'

- **ID:** `mongodb/transaction-api-version-error`
- **领域:** mongodb
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

驱动程序或应用程序尝试使用不受支持的事务 API 版本，通常是由于驱动程序/库版本与 MongoDB 服务器不匹配。

## 版本兼容性

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

## 解决方案

1. ```
   升级 MongoDB 驱动程序到与服务器 API 版本兼容的版本。对于 Node.js：npm install mongodb@6.3.0。对于 Python：pip install pymongo==4.6.0。
   ```
2. ```
   在连接字符串中显式设置 API 版本：mongodb://localhost:27017/?apiVersion=1 以强制兼容性。
   ```

## 无效尝试

- **** — Upgrading the MongoDB server to the latest version doesn't fix the mismatch if the driver is outdated. (80% 失败率)
- **** — Setting transaction options to empty object in the driver is ignored because the API version is negotiated at connection time. (90% 失败率)
