Docs HomeMongoDB Manual

Stable API

What is the Stable API, and Should You Use It?什么是稳定的API?你应该使用它吗?

The MongoDB Stable API (previously labeled the Versioned API) lets you upgrade your MongoDB server at will, and ensure that behavior changes between MongoDB versions do not break your application.MongoDB Stable API(之前标记为Versioned API)允许您随意升级MongoDB服务器,并确保MongoDB版本之间的行为变化不会破坏您的应用程序。

MongoDB 5.0 introduces the Stable API for applications communicating with MongoDB server products. The Stable API allows you to specify which version of the MongoDB API your application runs against.MongoDB 5.0为与MongoDB服务器产品通信的应用程序引入了Stable API。Stable API允许您指定应用程序运行的MongoDB API版本。

The Stable API provides long-term API stability for applications and supports more frequent releases and automatic server upgrades. 稳定的API为应用程序提供了长期的API稳定性,并支持更频繁的发布和服务器自动升级。This allows your applications to take advantage of rapidly released features without risking backwards-breaking changes.这使您的应用程序能够利用快速发布的功能,而不会冒着向后破坏更改的风险。

The default behavior for your driver connection will continue to function as expected, even if you do not explicitly specify an apiVersion.驱动程序连接的默认行为将继续按预期运行,即使您没有明确指定apiVersion

The Stable API encompasses the subset of MongoDB commands that applications use to read and write data, create collections and indexes, and perform other common tasks.Stable API包含应用程序用来读写数据、创建集合和索引以及执行其他常见任务的MongoDB命令的子集

Note

Starting in February 2022, the "Versioned API" terminology was changed to "Stable API". 从2022年2月开始,“有版本的API”术语更改为“稳定的API”。All concepts and features remain the same with this naming change.所有的概念和功能在这次命名更改后保持不变。

Backward Compatibility Guarantee向后兼容性保证

Your application will not experience significant behavior changes resulting from server upgrades. 您的应用程序不会因服务器升级而发生显著的行为更改。This guarantee holds as long as the new server supports your specified API version.只要新服务器支持您指定的API版本,此保证就有效。

To guarantee backward compatibility, your application must:为了保证向后兼容性,您的应用程序必须:

  • Declare an API version声明API版本
  • Only use commands and features supported in your specified API version仅使用指定的API版本中支持的命令和功能
  • Deploy with a supported version of an official driver使用受支持的官方驱动程序版本进行部署

Declare the API Version声明API版本


➤ Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.


To use the Stable API, upgrade to the latest driver and create your application's MongoClient:要使用Stable API,请升级到最新的驱动程序并创建应用程序的MongoClient:

client = new MongoClient(uri, { serverApi: { version: '1' } });

"1" is currently the only API version available.是目前唯一可用的API版本。

By default, clients are non-strict. 默认情况下,客户端是非严格的A non-strict client allows you to run any command, regardless of whether or not it belongs to the Stable API.非限制客户端允许您运行任何命令,无论它是否属于Stable API。

Checking Client API Versions检查客户端API版本

Use the serverStatus command to check for your application's configured API version. 使用serverStatus命令检查应用程序配置的API版本。For each application connected to your MongoDB instance, an appname appears in the apiVersions document.对于连接到MongoDB实例的每个应用程序,apiVersions文档中都会显示一个appname

See metrics.apiVersions for more information.

db.runCommand( { serverStatus: 1 } ).metrics.apiVersions

Create a Strict Client创建严格客户端

A strict client rejects all commands outside of the Stable API. 严格的客户端拒绝稳定API之外的所有命令。Attempts to use commands outside of the Stable API will receive the APIVersionError response.尝试使用Stable API之外的命令将收到APIVersionError响应。

Use the sample code to create a strict client:使用示例代码创建一个严格的客户端:

client = new MongoClient(uri, { serverApi: { version: '1', strict: true } });

Migrate To Stable API Commands移植到稳定的API命令

To migrate your application to use the Stable API, you must:要迁移应用程序以使用Stable API,您必须:

  1. Run your application's test suite with the new MongoClient options.使用新的MongoClient选项运行应用程序的测试套件。
  2. Determine which commands and features you're using that are outside of the Stable API.确定您正在使用的是稳定API之外的命令和功能。
  3. Migrate to alternative commands and features in the Stable API.迁移到Stable API中的替代命令和功能。

Once your application uses only commands and features defined in the Stable API, you can redeploy it with the new MongoClient options and be confident that future server upgrades won't negatively impact your application.一旦您的应用程序仅使用Stable API中定义的命令和功能,您就可以使用新的MongoClient选项重新部署它,并确信未来的服务器升级不会对您的应用软件产生负面影响。

How To Use Commands and Features Outside of the Stable API如何在稳定的API之外使用命令和功能

To use commands and features outside of the Stable API, you can connect to your deployment with a non-strict client. 要使用Stable API之外的命令和功能,可以使用非限制客户端连接到部署。By default, clients are non-strict.默认情况下,客户端是非严格的

To create a non-strict client, use the following sample code:要创建非严格客户端,请使用以下示例代码:

client = new MongoClient(uri, { serverApi: { version: '1', strict: false } });

Using this non-strict client allows you to run commands outside of the Stable API. 使用这个非限制客户端可以在Stable API之外运行命令。For example, this non-strict client allows you to run the createUser command.例如,这个非严格客户端允许您运行createUser命令。

Important

Commands and features outside of the Stable API do not have the same backward compatibility guarantees as versioned alternatives.Stable API之外的命令和功能与版本化的替代方案没有相同的向后兼容性保证。

Stable API Commands稳定的API命令

The database commands included in Stable API V1 depend on the MongoDB version you are using. Stable API V1中包含的数据库命令取决于您使用的MongoDB版本。To view the database commands included in the Stable API and the MongoDB version they were introduced, see Stable API Changelog.要查看Stable API中包含的数据库命令及其引入的MongoDB版本,请参阅Stable API Changelog

Parameters参数

You can specify the following optional parameters for Stable API in your application's MongoDB driver connection code. 您可以在应用程序的MongoDB驱动程序连接代码中为Stable API指定以下可选参数。Check the MongoDB driver documentation for the driver you use in your application for more information:有关更多信息,请查看应用程序中使用的驱动程序的MongoDB驱动程序文档:

Parameter参数Type类型Description描述
apiVersionstringSpecifies the API Version. 指定API版本。"1" is currently the only supported version.是当前唯一受支持的版本。
apiStrictbooleanIf true, using a command that is not part of the declared API version returns an APIStrictError error. 如果为true,则使用不是声明的API版本的一部分的命令将返回APIStrictError错误。If you specify apiStrict, you must also specify apiVersion.如果指定apiStrict,则还必须指定apiVersion
If not specified, defaults to false. 如果未指定,则默认为false
apiDeprecationErrorsbooleanIf true, using a command or behavior that is deprecated in the specified API version returns an APIDeprecationError. 如果为true,则使用指定API版本中不推荐使用的命令或行为将返回APIDeprecationErrorIf you specify apiDeprecationErrors, you must also specify apiVersion.如果指定apiDeprecationErrors,则还必须指定apiVersion
If not specified, defaults to false. 如果未指定,则默认为false

Behavior行为

Parameter Validation参数验证

Starting in MongoDB 5.0, API V1 database commands raise an error if passed a parameter not explicitly accepted by the command.从MongoDB 5.0开始,如果传递了命令未明确接受的参数,API V1数据库命令将引发错误。

In MongoDB 4.4 and earlier, unrecognized parameters are silently ignored.在MongoDB 4.4及更早版本中,无法识别的参数被默默忽略。

Stable API Error Responses稳定的API错误响应

This table shows error responses for problematic Stable API requests.此表显示有问题的Stable API请求的错误响应。

Server Response服务器响应Request要求
APIDeprecationErrorSpecifies { apiDeprecationErrors: true } with API version V and uses a behavior deprecated in V. 使用API版本V指定{ apiDeprecationErrors: true },并使用V中不推荐使用的行为。
APIStrictErrorSpecifies { apiStrict: true } with API version V, but uses a behavior not in version V. 使用API版本V指定{ apiStrict: true },但在版本V中未使用行为。
APIVersionErrorSpecifies an apiVersion that the server does not support. 指定服务器不支持的apiVersion
InvalidOptionsSpecifies { apiStrict: true } or { apiDeprecationErrors: true } but omits apiVersion. 指定{ apiStrict: true }{ apiDeprecationErrors: true },但省略apiVersion