Stable API
On this page本页内容
The Stable API feature requires MongoDB Server 5.0 or later.Stable API功能需要MongoDB Server 5.0或更高版本。
You should only use the Stable API feature if all the MongoDB servers you are connecting to support this feature.如果您正在连接的所有MongoDB服务器都支持Stable API功能,则只应使用该功能。
Overview概述
In this guide, you can learn how to specify the Stable API when connecting to a MongoDB instance or replica set. 在本指南中,您可以学习如何在连接到MongoDB实例或副本集时指定Stable API。You can use the Stable API feature to force the server to run operations with behavior compatible with the specified API version. 您可以使用Stable API功能强一致性务器运行行为与指定API版本兼容的操作。An API version defines the expected behavior of the operations it covers and the format of server responses. API版本定义了它所涵盖的操作的预期行为以及服务器响应的格式。If you change to a different API version, the operations are not guaranteed to be compatible and the server responses are not guaranteed to be similar.如果更改为不同的API版本,则不能保证操作是兼容的,也不能保证服务器响应是相似的。
When you use the Stable API feature with an official MongoDB driver, you can update your driver or server without worrying about backward compatibility issues of the commands covered by the Stable API.当您将Stable API功能与官方MongoDB驱动程序一起使用时,您可以更新驱动程序或服务器,而无需担心Stable API所涵盖命令的向后兼容性问题。
See the MongoDB reference page on the Stable API for more information including a list of commands it covers.请参阅Stable API上的MongoDB参考页面,了解更多信息,包括它包含的命令列表。
The following sections describe how you can enable the Stable API for your MongoDB client and the options that you can specify.以下部分描述了如何为MongoDB客户端启用Stable API以及可以指定的选项。
Enable the Stable API on a MongoDB Client在MongoDB客户端上启用稳定的API
To enable the Stable API, you must specify an API version in the 要启用Stable API,必须在传递给MongoClientOptions
passed to your MongoClient
. MongoClient
的MongoClientOptions
中指定API版本。Once you instantiate a 使用指定的API版本实例化MongoClient
instance with a specified API version, all commands you run with that client use that version of the Stable API.MongoClient
实例后,使用该客户端运行的所有命令都将使用该版本的Stable API。
If you need to run commands using more than one version of the Stable API, instantiate a separate client with that version.如果您需要使用一个以上版本的Stable API运行命令,请使用该版本实例化一个单独的客户端。
If you need to run commands not covered by the Stable API, make sure the "strict" option is disabled. 如果您需要运行Stable API未涵盖的命令,请确保禁用“严格”选项。See the section on Stable API Options for more information.有关更多信息,请参阅稳定API选项部分。
The example below shows how you can instantiate a 下面的示例展示了如何实例化MongoClient
that sets the Stable API version and connects to a server by performing the following operations:MongoClient
,该客户端设置Stable API版本并通过执行以下操作连接到服务器:
Specify a server URI to connect to.指定要连接到的服务器URI。Specify a Stable API version in the使用MongoClientOptions
object, using a constant from theServerApiVersion
object.ServerApiVersion
对象中的常量,在MongoClientOptions
对象中指定稳定的API版本。Instantiate a实例化MongoClient
, passing the URI and theMongoClientOptions
to the constructor.MongoClient
,将URI和MongoClientOptions
传递给构造函数。
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
const client = new MongoClient(uri, { serverApi: ServerApiVersion.v1 });
If you specify an API version and connect to a MongoDB server that does not support the Stable API, your application may throw an error when connecting to your MongoDB server with the following text:如果您指定了API版本并连接到不支持Stable API的MongoDB服务器,则您的应用程序在连接到MongoDB服务器时可能会抛出错误,并显示以下文本:
MongoParseError: Invalid server API version=...
For more information on the methods and classes referenced in this section, see the following API Documentation:有关本节中引用的方法和类的更多信息,请参阅以下API文档:
Stable API Options稳定的API选项
You can enable or disable optional behavior related to the Stable API as described in the following table.您可以启用或禁用与稳定API相关的可选行为,如下表所述。
version | Default: null |
---|---|
strict | Default: false |
deprecationErrors | Default: false |
The following example shows how you can set the options of the 以下示例显示了如何设置ServerApi
interface.ServerApi
接口的选项。
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&w=majority";
const client = new MongoClient(uri,
{
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
For more information on the options in this section, see the following API Documentation:有关本节中选项的更多信息,请参阅以下API文档: