

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon DocumentDB 与 MongoDB 兼容性
<a name="compatibility"></a>

亚马逊 DocumentDB 支持兼容 MongoDB，包括 MongoDB 4.0、mongoDB 5.0 和 MongoDB 8.0。MongoDB 兼容性意味着，您目前配合 MongoDB 数据库使用的绝大多数应用程序、驱动程序和工具都可以配合 Amazon DocumentDB 一起使用，变化甚微或无变化。本节说明了您需要了解的关于 Amazon DocumentDB 与 MongoDB 兼容性的一切，包括新功能和新特征、入门、迁移路径和功能差异。

**Topics**
+ [兼容 MongoDB 8.0](#mongodb-80)
+ [MongoDB 5.0 兼容性](#mongodb-50)
+ [与 MongoDB 4.0 的兼容性](#mongodb-40)

## 兼容 MongoDB 8.0
<a name="mongodb-80"></a>

**Topics**
+ [亚马逊 DocumentDB 8.0 的新增功能](#compatibility-whatsnew-8)
+ [开始使用 Amazon DocumentDB](#compatibility-getstarted-5)
+ [升级或迁移到亚马逊 DocumentDB 5.0 或 8.0](#compatibility-upgrade-5)
+ [功能差异](#compatibility-differences-5)

### 亚马逊 DocumentDB 8.0 的新增功能
<a name="compatibility-whatsnew-8"></a>

Amazon DocumentDB 8.0 将查询性能提高多达 7 倍，压缩率提高多达 5 倍，使您能够以更低的成本构建高性能应用程序。亚马逊 DocumentDB 8.0 与 MongoDB 8.0 完全兼容。以下摘要介绍了亚马逊 DocumentDB 8.0 中引入的一些主要功能。要查看新功能的完整列表，请参阅 [发布说明](release-notes.md)。
+ 通过添加对 MongoDB 8.0 API 驱动程序的支持，提供与 MongoDB 8.0 的兼容性。还支持使用 MongoDB API 版本 6.0 和 7.0 构建的应用程序。
+ Amazon DocumentDB 8.0 中的新查询规划器将性能改进扩展到聚合阶段运算符，同时支持聚合管道优化和不同的命令。
+ 亚马逊 DocumentDB 8.0 支持排序规则和视图。
+ 提供 6 个新的聚合阶段：\$1replaceWith、\$1VectorSearch、\$1merge、\$1set、\$1unset、\$1unset、\$1bucket 和 3 个新的聚合运算符 \$1pow、\$1rand、\$1dateTrunc。
+ 新版本的文本索引：Amazon DocumentDB 8.0 中的文本索引 v2 引入了额外的标记，增强了文本搜索功能。
+ 通过并行向量索引构建，Amazon DocumentDB 8.0 最多可将索引构建时间缩短 30 倍。

### 开始使用亚马逊 DocumentDB 8.0
<a name="compatibility-getstarted-5"></a>

要开始使用亚马逊 DocumentDB 8.0，请参阅《[入](https://docs.aws.amazon.com/documentdb/latest/developerguide/get-started-guide.html)门指南》。您可以使用或 AWS 软件开发工具包创建新的 Amazon DocumentDB 8.0 集群， AWS 管理控制台 AWS CLI或者。 CloudFormation连接到 Amazon DocumentDB 时，要求您使用与 MongoDB 5.0 或更高版本兼容的 MongoDB 驱动程序或实用工具。

**注意**  
使用 AWS SDK AWS CLI、或时 CloudFormation，引擎版本将默认为 5.0.0。要创建新的 Amazon DocumentDB 8.0 集群、创建新的亚马逊 DocumentDB 4.0 集群或`engineVersion = 4.0.0`创建新的 Amazon DocumentDB 3.6 集群`engineVersion = 3.6.0`，您必须明确指定参数`engineVersion = 8.0.0`。对于给定的 Amazon DocumentDB 集群，您可以使用调用`describe-db-clusters`或使用 Amazon DocumentDB 管理控制台来查看特定集群的引擎版本号来确定集群版本。 AWS CLI 

Amazon DocumentDB 5.0 和 8.0 支持 Amazon EC2 `r8g` Graviton4 处理器和 Graviton2 处理器，例如您的集群的`t4.medium`实例类型，`r6g`并且适用于所有支持的区域（参见）。[实例](what-is.md#what-is-db-instances)有关定价的更多信息，请参阅 [Amazon DocumentDB（兼容 MongoDB）定价](https://aws.amazon.com/documentdb/pricing/)。

### 升级或迁移到亚马逊 DocumentDB 5.0 或 8.0
<a name="compatibility-upgrade-5"></a>

[你可以使用、、和之类的或实用程序从 MongoDB 3.6 或 MongoDB 4.0 迁移到亚马逊 DocumentDB 5.0 或 Amazon DocumentDB 8.0。[AWS DMS](https://aws.amazon.com/dms/)`mongodump``mongorestore``mongoimport``mongoexport`](https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html)有关如何迁移的说明，请参阅 [使用升级您的亚马逊文档数据库集群 AWS Database Migration Service](docdb-migration.versions.md)。

### 功能差异
<a name="compatibility-differences-5"></a>

#### 亚马逊 DocumentDB 5.0 和 8.0 之间的功能区别
<a name="w2aac11b9c11b3"></a>

随着亚马逊 DocumentDB 8.0 的发布，亚马逊 DocumentDB 5.0 和亚马逊 DocumentDB 8.0 之间存在功能差异：
+  Planner v1 是亚马逊 DocumentDB 5.0 中的默认查询计划器，而性能更高的 Planner v3 是亚马逊 DocumentDB 8.0 中的默认查询计划器。
+  亚马逊 DocumentDB 8.0 中的新功能，包括视图、排序规则和 \$1merge 等运算符，仅与 Planner v3 兼容。
+  在 Amazon DocumentDB 8.0 中，压缩功能默认处于开启状态，并设置为使用 Zstandard 算法。此外，在亚马逊 DocumentDB 8.0 中，“启用” 不再是有效的选择；您可以从 Zstd 中进行选择，但不选择。 LZ4 
+  无服务器目前在 Amazon DocumentDB 8.0 中不可用。

## MongoDB 5.0 兼容性
<a name="mongodb-50"></a>

**Topics**
+ [Amazon DocumentDB 5.0 中有什么新内容](#compatibility-whatsnew-5)
+ [Amazon DocumentDB 5.0 入门](#compatibility-getstarted-5)
+ [升级或迁移到亚马逊 DocumentDB 5.0](#compatibility-upgrade-5)
+ [功能差异](#compatibility-differences-5)

### Amazon DocumentDB 5.0 中有什么新内容
<a name="compatibility-whatsnew-5"></a>

Amazon DocumentDB 5.0 引入了新特征和新功能，其中包括存储限值和客户端字段级加密。以下摘要介绍了Amazon DocumentDB 5.0 中引入的一些主要特征。要查看新功能的完整列表，请参阅 [发布说明](release-notes.md)。
+ 对于所有基于实例的 Amazon DocumentDB 集群和基于分片的弹性集群，将存储限值升高到 128TiB。
+ 引入 Amazon DocumentDB 5.0 引擎（版本 3.0.775）
  + 支持 MongoDB 5.0 API 驱动程序
  + 支持客户端字段级加密 (FLE)。现在，在写入数据至 Amazon DocumentDB 集群之前，您可以在客户端对字段加密。有关更多信息，请参阅[客户端字段级加密](https://docs.aws.amazon.com/documentdb/latest/developerguide/field-level-encryption.html)。
  + 新聚合操作符：`$dateAdd`, `$dateSubtract` 
  + 支持带 `$elemMatch` 操作符的索引。因此，具有 `$elemMatch` 的查询将导致索引扫描。

Amazon DocumentDB 并不支持 MongoDB 5.0 的所有特征。构建 Amazon DocumentDB 5.0 时，我们从客户最迫切要求我们构建的特征和功能逆向工作。我们将根据客户要求我们构建的内容，持续添加其他 MongoDB 5.0 功能。有关支持的最新列表 APIs，请参阅[在 Amazon Document APIs DB 中支持 MongoDB、操作和数据类型](mongo-apis.md)。

### Amazon DocumentDB 5.0 入门
<a name="compatibility-getstarted-5"></a>

了解 Amazon DocumentDB 5.0 入门，请参阅[入门指南](https://docs.aws.amazon.com/documentdb/latest/developerguide/get-started-guide.html)。您可以使用或软件开发工具包、 AWS 管理控制台 AWS CLI或创建新的 Amazon Document AWS DB 5.0 集群。 CloudFormation连接到 Amazon DocumentDB 时，要求您使用与 MongoDB 5.0 或更高版本兼容的 MongoDB 驱动程序或实用工具。

**注意**  
使用 AWS SDK AWS CLI、或时 CloudFormation，引擎版本将默认为 5.0.0。您必须明确指定参数 `engineVersion = 4.0.0` 以创建新的 Amazon DocumentDB 4.0 集群 或参数 `engineVersion = 3.6.0` 以创建新的 Amazon DocumentDB 3.6 集群。对于给定的 Amazon DocumentDB 集群，您可以使用调用`describe-db-clusters`或使用 Amazon DocumentDB 管理控制台来查看特定集群的引擎版本号来确定集群版本。 AWS CLI 

Amazon DocumentDB 5.0 和 8.0 支持 Amazon EC2 `r8g` Graviton4 处理器和 Graviton2 处理器，例如您的集群的`t4.medium`实例类型，`r6g`并且适用于所有支持的区域（参见）。[实例](what-is.md#what-is-db-instances)有关定价的更多信息，请参阅 [Amazon DocumentDB（兼容 MongoDB）定价](https://aws.amazon.com/documentdb/pricing/)。

### 升级或迁移到亚马逊 DocumentDB 5.0
<a name="compatibility-upgrade-5"></a>

您可以使用 [AWS DMS](https://aws.amazon.com/dms/) 或实用工具如 [`mongodump`、`mongorestore`、`mongoimport` 和 `mongoexport`](https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html) 从 MongoDB 3.6 或 MongoDB 4.0 迁移到 Amazon DocumentDB 5.0。有关如何迁移的说明，请参阅 [使用升级您的亚马逊文档数据库集群 AWS Database Migration Service](docdb-migration.versions.md)。

### 功能差异
<a name="compatibility-differences-5"></a>

#### Amazon DocumentDB 4.0 与 5.0 之间的功能差异
<a name="w2aac11c13c11b3"></a>

随着 Amazon DocumentDB 5.0 发布，Amazon DocumentDB 4.0 与 Amazon DocumentDB 5.0 之间存在功能差异：
+ 备份内置角色现在支持 `serverStatus`。操作：具有备份角色的开发人员和应用程序可以收集有关 Amazon DocumentDB 集群状态的统计量。
+ `SecondaryDelaySecs` 字段替换 `replSetGetConfig`输出中的 `slaveDelay`。
+ **hello** 命令替换：`isMaster`**hello**返回描述 Amazon DocumentDB 集群角色的文档。
+ Amazon DocumentDB 5.0 现在支持在第一个嵌套级别采用 `$elemMatch` 操作符的索引扫描。如果仅查询筛选器具有一个级别的 `$elemMatch` 筛选器，则支持索引扫描，但如果包含一个嵌套 `$elemMatch` 查询，则不支持索引扫描。

  例如，在 Amazon DocumentDB 5.0 中，如果您在嵌套级别中包含 `$elemMatch` 操作符，将不会像在 Amazon DocumentDB 4.0 中那样返回数值：

  ```
  db.foo.insert(
  [
      {a: {b: 5}},
      {a: {b: [5]}},
      {a: {b: [3, 7]}},
      {a: [{b: 5}]},
      {a: [{b: 3}, {b: 7}]},
      {a: [{b: [5]}]},
      {a: [{b: [3, 7]}]},
      {a: [[{b: 5}]]},
      {a: [[{b: 3}, {b: 7}]]},
      {a: [[{b: [5]}]]},
      {a: [[{b: [3, 7]}]]}
  ]);
  
  // DocumentDB 5.0
  > db.foo.find({a: {$elemMatch: {b: {$elemMatch: {$lt: 6, $gt: 4}}}}}, {_id: 0})
  { "a" : [ { "b" : [ 5 ] } ] }
  
  // DocumentDB 4.0
  > db.foo.find({a: {$elemMatch: {b: {$elemMatch: {$lt: 6, $gt: 4}}}}}, {_id: 0})
  { "a" : [ { "b" : [ 5 ] } ] }
  { "a" : [ [ { "b" : [ 5 ] } ] ] }
  ```
+ Amazon DocumentDB 4.0 中的“\$1”投影返回带所有字段的所有文档。在 Amazon DocumentDB 5.0 中，带“\$1”投影的 **find** 命令返回匹配查询参数的文档，这种参数仅含有匹配“\$1”投影的字段。
+ 在 Amazon DocumentDB 5.0 中，带 `$regex` 和 `$options` 查询参数的 **find** 命令返回错误：“无法在 `$regex` 和 `$options` 同时设置选项”。
+ 在 Amazon DocumentDB 5.0 中，目前 `$indexOfCP` 在以下情况时返回“-1”：
  + 字符串表达式中未找到子字符串，或
  + 起点是一个大于终点的数字，或
  + 起点是大于字符串字节长度的数字。
+ 在 Amazon DocumentDB 4.0 中，当起点位置是大于终点或大于字符串字节长度的数字，`$indexOfCP` 返回 “0”。
+ 在 Amazon DocumentDB 5.0 中，在 `_id fields` 中的投影操作，例如 `{"_id.nestedField" : 1}`，返回仅包含所投影字段的文档。而在 Amazon DocumentDB 4.0 中，嵌套字段投影命令不筛选出任何文档。

## 与 MongoDB 4.0 的兼容性
<a name="mongodb-40"></a>

**Topics**
+ [Amazon DocumentDB 4.0 特征](#compatibility-whatsnew)
+ [Amazon DocumentDB 4.0 入门](#compatibility-getstarted)
+ [升级或迁移到 Amazon DocumentDB 4.0](#compatibility-upgrade)
+ [功能差异](#compatibility-differences)

### Amazon DocumentDB 4.0 特征
<a name="compatibility-whatsnew"></a>

Amazon DocumentDB 4.0 引入了许多新特征和新功能，包括 ACID 事务和对变更流的改进。以下摘要显示了 Amazon DocumentDB 4.0 中引入的一些主要特征。要查看功能的完整列表，请参阅 [发布说明](release-notes.md)。
+ **ACID 事务**：Amazon DocumentDB 现在支持跨多个文档、报表、集合和数据库执行事务的能力。事务通过使您能够在 Amazon DocumentDB 集群内部跨一个或多个文档执行原子操作、一致操作、隔离操作和持久操作（ACID），简化应用程序开发。有关更多信息，请参阅 [Amazon DocumentDB 中的事务](transactions.md)。
+ **变更流**：您现在有能力在集群层面（`client.watch()` 或 `mongo.watch()`）和在数据库（`db.watch()`）打开变更流，可以指定 `startAtOperationTime` 来打开变更流游标，并且最后，您现在可以将变更流保留期延长至 7 天（之前为 24 小时）。有关更多信息，请参阅 [将变更流与 Amazon DocumentDB 结合使用](change_streams.md)。
+ **AWS Database Migration Service**(AWS DMS)：您现在可以使用将 MongoDB 4.0 AWS DMS 工作负载迁移到亚马逊 DocumentDB。 AWS DMS 现在支持 MongoDB 4.0 源、亚马逊 DocumentDB 4.0 目标和亚马逊 DocumentDB 3.6 源，用于在亚马逊 DocumentDB 3.6 和 4.0 之间执行升级。有关更多信息，请参阅 [AWS DMS 文档](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html)。
+ **性能和索引化**：现在，您可以利用带 `$lookup` 的索引，查找查询借助含一个字段或一个字段且 `_id` 字段可以从索引中直接提供而无需从集合（涵盖的查询）中读取，借助 `findAndModify` 进行 `hint()` 的能力、对 `$addToSet` 的性能优化和缩小索引总体大小的改进。有关更多信息，请参阅 [发布说明](release-notes.md)。
+ **操作符**：Amazon DocumentDB 4.0 现在支持许多新的聚合操作符：`$ifNull`、`$replaceRoot`、`$setIsSubset`、`$setIntersection`、`$setUnion`、`$setEquals`。您可以在上查看我们支持的所有M APIs ongoDB、操作和数据类型。[在 Amazon Document APIs DB 中支持 MongoDB、操作和数据类型](mongo-apis.md)
+ **基于角色的访问控制**（RBAC）：同时使用命令 `ListCollection` 和 `ListDatabase` ，您现在可以选择使用 `authorizedCollections` 和 `authorizedDatabases` 参数允许用户列出他们有权访问的集合和数据库，而无需分别使用 `listCollections` 和 `listDatabase` 角色。您还可以杀死自己的游标，而无需使用 `KillCursor` 角色。

Amazon DocumentDB 并不支持 MongoDB 4.0 的所有特征。构建 Amazon DocumentDB 4.0 时，我们从客户最迫切要求我们构建的特征和功能逆向工作。我们将根据客户要求我们构建的内容，持续添加其他 MongoDB 4.0 功能。例如，Amazon DocumentDB 4.0 目前不支持 MongoDB 4.0 中引入的类型转换操作符或字符串操作符。有关支持的最新列表 APIs，请参阅[在 Amazon Document APIs DB 中支持 MongoDB、操作和数据类型](mongo-apis.md)。

### Amazon DocumentDB 4.0 入门
<a name="compatibility-getstarted"></a>

了解 Amazon DocumentDB 4.0 入门，请参阅[入门指南](https://docs.aws.amazon.com/documentdb/latest/developerguide/get-started-guide.html)。您可以使用或软件开发工具包创建新的 Amazon Document AWS DB 4.0 集群 AWS CLI， AWS 管理控制台 或。 CloudFormation连接到 Amazon DocumentDB 时，要求您使用与 MongoDB 4.0 或更高版本兼容的 MongoDB 驱动程序或实用工具。

**注意**  
使用 AWS SDK AWS CLI、或时 CloudFormation，引擎版本将默认为 5.0.0。您必须明确指定参数 `engineVersion = 4.0.0` 以创建新的 Amazon DocumentDB 4.0 集群 或参数 `engineVersion = 3.6.0` 以创建新的 Amazon DocumentDB 3.6 集群。对于给定的 Amazon DocumentDB 集群，您可以使用调用`describe-db-clusters`或使用 Amazon DocumentDB 管理控制台来查看特定集群的引擎版本号来确定集群版本。 AWS CLI 

Amazon DocumentDB 4.0 支持您集群的 `r5`、`r6g`、`t3.medium` 和 `t4g.medium` 实例类型，并且可用于所有受支持区域。没有使用 Amazon DocumentDB 4.0 的额外成本。有关定价的更多信息，请参阅 [Amazon DocumentDB（与 MongoDB 兼容）定价](https://aws.amazon.com/documentdb/pricing/)。

### 升级或迁移到 Amazon DocumentDB 4.0
<a name="compatibility-upgrade"></a>

您可以使用 [AWS DMS](https://aws.amazon.com/dms/) 或实用工具如 [`mongodump`、`mongorestore`、`mongoimport` 和 `mongoexport`](https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html) 从 MongoDB 3.6 或 MongoDB 4.0 迁移到 Amazon DocumentDB 4.0。类似地，您可以使用相同工具从 Amazon DocumentDB 3.6 升级到 Amazon DocumentDB 4.0。有关如何迁移的说明，请参阅 [使用升级您的亚马逊文档数据库集群 AWS Database Migration Service](docdb-migration.versions.md)。

### 功能差异
<a name="compatibility-differences"></a>

#### Amazon DocumentDB 3.6 与 4.0 之间的功能差异
<a name="w2aac11c15c11b3"></a>

随着 Amazon DocumentDB 4.0 发布，Amazon DocumentDB 3.6 和Amazon DocumentDB 4.0 之间存在功能差异：
+ **嵌套文档的投影**：Amazon DocumentDB 3.6 在应用投影时考虑嵌套文档中的第一个字段。但是，Amazon DocumentDB 4.0 将解析子文档并将投影也应用于每个子文档。例如：如果投影是 `"a.b.c": 1`，则行为在两个版本中相同。但是，如果投影是 `{a:{b:{c:1}}}`，则 Amazon DocumentDB 3.6 将仅将该投影应用于“a”，而非“b”或“c”。
+ **对 `minKey`、`maxKey`的行为**：在 Amazon DocumentDB 4.0 中，对 `{x:{$gt:MaxKey}}` 的行为不返回任何内容，对 `{x:{$lt:MaxKey}}` 的行为返回所有内容。
+ **文档比较差异**：比较子文档（例如，`{"_id" :1, "a" :{"b":1}}` 中 `b`）中不同类型（double、int、 long）的数值现在跨不同数字数据类型并为文档的每个级别提供一致输出。

#### Amazon DocumentDB 4.0 与 MongoDB 4.0 之间的功能差异
<a name="w2aac11c15c11b5"></a>

以下是 Amazon DocumentDB 4.0 与 MongoDB 4.0 之间的功能差异。
+ **在路径中借助空键查找**：当集合包含数组内部带空键的文档（例如`{"x" : [ { "" : 10 }, { "b" : 20 } ]}`），且查询中所用的密钥以空字符串结尾（例如`x.`）时，则 Amazon DocumentDB 将返回该文档，因为其遍历该数组中的所有文档，而 MongoDB 将不返回该文档。
+ **路径中的 `$setOnInsert` 同 `$` **：在 Amazon DocumentDB 中字段操作符 `$setOnInsert` 将与 `$` 在路径中共同发挥作用，这也与 MongoDB 4.0 一致。