Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /

setFeatureCompatibilityVersion(数据库命令)

setFeatureCompatibilityVersion

启用或禁用将与早期版本 MongoDB 不兼容的数据持久化的功能。您只能对 admin 数据库发出 setFeatureCompatibilityVersion

警告

启用向后不兼容的功能可能会使降级过程变得复杂,因为在降级之前,您必须删除任何持续存在的向后不兼容的功能。

升级后,建议让部署在不启用向后不兼容功能的情况下稳定运行一段时间,以确保降级回老版本的可能性极小。当确信降级的可能性非常小时,请启用这些功能。

此命令可用于以下环境中托管的部署:

  • MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本

  • MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本

在版本8.3中进行了更改

该命令具有以下语法:

db.adminCommand(
{
setFeatureCompatibilityVersion: <version>,
confirm: true,
writeConcern: { wtimeout: <timeout> },
dryRun: <boolean>
}
)

setFeatureCompatibilityVersion 命令接受以下字段:

必需

version 的可能值是:

版本
说明

"8.3"

"8.2"

可用于MongoDB 8.2 和MongoDB 8.3部署

Starting in MongoDB 8.3, you can downgrade FCV from "8.3" to "8.2".

"8.0"

"7.0"

必需

7.0 版本中的新增功能

设置为 true 以确认功能兼容性更改并允许操作继续。

If you omit the confirm parameter or set confirm to a value other than true, the command fails and returns a warning about modifying the feature compatibility version.

Optional

writeConcern 以毫秒为单位指定写关注 wtimeout 值:

  • 主节点等待大多数副本集成员确认的时间段。如果在该时间段内未收到确认,则操作失败。

  • 默认值为 60000 毫秒。如果副本集的次要成员的延迟超过 wtimeout 默认值,则使用更长的时间段。

注意

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

Optional

If set to true, MongoDB simulates an upgrade or downgrade of the feature compatibility version. If the cluster contains incompatible data, the operation fails with an error.

8.3版本新增

如果必须将功能兼容性版本降级到 8.0 以下,则必须先运行 transitionToDedicatedConfigServer 命令。有关降级的详细信息,请参阅功能兼容性版本。

If you try to upgrade the FCV of a cluster that contains forwards-incompatible data in the upgraded version, you receive a CannotUpgrade error. Forwards-incompatible data can refer to any data in your cluster that relies on a feature that was removed in the target version.

When this error occurs, either:

  • Modify your cluster data to remove forwards-incompatible features, then re-run the setFeatureCompatibilityVersion command with the upgraded version to set the FCV to the upgraded version.

  • Run the setFeatureCompatibilityVersion command with the original downgraded version to set the FCV back to the original version.

    重要

    Setting the FCV to the original version stops the upgrade procedure and changes the FCV back to the downgraded version. This procedure does not reset the cluster back to the state before the FCV upgrade began.

    If the FCV upgrade confirms that there is no forwards-incompatible data but otherwise stops or fails, any subsequent FCV downgrade attempts will also fail with an error message. You must complete the FCV upgrade before trying to downgrade the FCV.

If you try to downgrade the FCV of a cluster that contains backwards-incompatible data in the downgraded version, you receive a CannotDowngrade error. Backwards-incompatible data can refer to any data in your cluster that relies on a feature that isn't available in the target version.

When this error occurs, either:

  • 修改集群数据以删除向后兼容的功能,然后使用降级版本重新运行 setFeatureCompatibilityVersion 命令,将 fCV 设置为降级版本。

  • 使用原始升级版本运行 setFeatureCompatibilityVersion 命令,以便将 fCV 重新设置为原始版本。

    重要

    FcV 设置为原始版本会停止降级过程,并将 FcV 更改回升级版本。此过程不会将集群重置回到 FcV 降级开始之前的状态。

    If the FCV downgrade confirms that there is no backwards-incompatible data but otherwise stops or fails, any subsequent FCV upgrade attempts will also fail with an error message. You must complete the FCV downgrade before trying to upgrade the FCV.

Starting in 8.3, you can downgrade your deployment's FCV to the immediately previous minor version.

要学习;了解更多信息,请参阅将8.3降级至8.2 。

某些后台操作可能会阻止 setFeatureCompatibilityVersion 的执行。使用 currentOp 确定任何正在进行的操作。

如果在初始同步过程中触发 setFeatureCompatibilityVersion 更改,在 oplog 应用阶段重放条目时,同步可能会失败,并显示 OplogOperationUnsupported 错误信息。此尝试之后的同步会成功,因为操作阶段不再重放该操作。

部署
featureCompatibilityVersion

对于新的8.3 部署

"8.3"

For 8.3 deployments upgraded from 8.2

"8.2" 直到您setFeatureCompatibilityVersion"8.3"

对于新的8.0 部署

"8.0"

For 8.0 deployments upgraded from 7.0

"7.0" 直到您setFeatureCompatibilityVersion"8.0"

对于新的 7.0 部署

"7.0"

"6.0" 直到您setFeatureCompatibilityVersion"7.0"

此命令执行的必须是对内部系统集合的写入。如果由于任何原因该命令未能成功完成,您都可以安全地重试该命令,因为该操作是幂等的。

从 MongoDB 6.0 开始,如果需要降级特征兼容性版本,请确保禁用集群到集群复制和用户写入阻止。

  1. 如果您启用了集群到集群复制,请将其禁用。

  2. 如果启用了用户写入阻止,请将其禁用:

    db.runCommand( { setUserWriteBlockMode: 1, global: false } )
  3. 等待上一条命令完成。

  4. 使用 setFeatureCompatibilityVersion 降级特征兼容性版本。

有关 MongoDB Cluster-to-Cluster Sync 的更多信息,请参阅文档

仲裁节点不会复制 admin.system.version 集合。因此,无论副本集的 fCV 值如何,仲裁节点始终具有等于二进制文件的降级版本的特征兼容性版本。

示例, MongoDB 5.0集群中的仲裁节点的FCV值为 4.4。

要查看 mongod 实例的 featureCompatibilityVersion,请在 mongod 实例上运行 getParameter 命令:

db.adminCommand(
{
getParameter: 1,
featureCompatibilityVersion: 1
}
)

输出类似如下所示:

{
featureCompatibilityVersion: { version: '5.0' },
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1660318752, i: 5 }),
signature: {
hash: Binary(Buffer.from("ce0cff3621e9b089fa6d8e9a1e1efc1a1ff15dab", "hex"), 0),
keyId: Long("7129893797260951557")
}
},
operationTime: Timestamp({ t: 1660318752, i: 5 })
}

注意

该操作未在 mongos 实例中定义。

在启用了访问控制的分片集群上,必须以分片本地用户身份连接到分片才能运行命令。

To enable the 8.0 features that persist data incompatible with MongoDB 7.0, set the feature compatibility to "8.0" on the MongoDB 8.0 deployment:

注意

admin 数据库运行 setFeatureCompatibilityVersion 命令。

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

db.adminCommand(
{
setFeatureCompatibilityVersion: "8.0",
confirm: true
}
)

To disable the 8.0 features that persist data incompatible with MongoDB 7.0, set the feature compatibility to "7.0" on the MongoDB 8.0 deployment:

注意

admin 数据库运行 setFeatureCompatibilityVersion 命令。

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

  • "7.0" featureCompatibilityVersion is supported on MongoDB 7.0 and MongoDB 8.0 deployments.

db.adminCommand(
{
setFeatureCompatibilityVersion: "7.0",
confirm: true
}
)

If you run this command as part of the downgrade process from MongoDB 8.0 to MongoDB 7.0, you must also remove all persisted features that are incompatible with 7.0. See the appropriate downgrade procedures.

要启用保留与 MongoDB 6.0 不兼容的数据的 7.0 功能,请在 MongoDB 7.0 部署上将功能兼容性设置为"7.0"

注意

admin 数据库运行 setFeatureCompatibilityVersion 命令。

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

db.adminCommand(
{
setFeatureCompatibilityVersion: "7.0",
confirm: true
}
)

要禁用保留与 MongoDB 6.0 不兼容的数据的 7.0 功能,请在 MongoDB 7.0 部署上将功能兼容性设置为"6.0"

注意

admin 数据库运行 setFeatureCompatibilityVersion 命令。

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

  • "6.0" 仅 MongoDB 6.0 和 MongoDB 7.0 部署支持 featureCompatibilityVersion。

db.adminCommand(
{
setFeatureCompatibilityVersion: "6.0",
confirm: true
}
)

如果作为从 MongoDB 7.0 降级到 MongoDB 6.0 的过程的一部分运行,则还必须删除所有与 6.0 不兼容的保留功能。请参阅相应的降级程序。

以下示例将可选写关注 wtimeout 字段设置为 5000(5 秒)。

注意

admin 数据库运行 setFeatureCompatibilityVersion 命令。

  • 对于独立实例,请在独立 mongod 实例上运行该命令。

  • 对于副本集,请在主节点上运行命令。大多数承载数据的成员必须可用。

  • 对于分片集群,请在 mongos 实例上运行该命令。

db.adminCommand( {
setFeatureCompatibilityVersion: "5.0",
writeConcern: { wtimeout: 5000 }
} )

后退

setClusterParameter