MongoDB 兼容性 - Amazon DocumentDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

MongoDB 兼容性

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

MongoDB 5.0 兼容性

Amazon DocumentDB 5.0 中有什么新内容

Amazon DocumentDB 5.0 引入了新特征和新功能,其中包括存储限值和客户端字段级加密。以下摘要介绍了Amazon DocumentDB 5.0 中引入的一些主要特征。要查看新功能的完整列表,请参阅 发布说明

  • 对于所有基于实例的 Amazon DocumentDB 集群和基于分片的弹性集群,将存储限值升高到 128 TiB。

  • 引入 Amazon DocumentDB 5.0 引擎(版本 3.0.775)

    • 支持 MongoDB 5.0 API 驱动程序

    • 支持客户端字段级加密 (FLE)。现在,在写入数据至 Amazon DocumentDB 集群之前,您可以在客户端对字段加密。有关更多信息,请参阅 客户端字段级加密

    • 新聚合操作符:$dateAdd, $dateSubtract

    • 支持带 $elemMatch 操作符的索引。因此,具有 $elemMatch 的查询将导致索引扫描。

Amazon DocumentDB 并不支持 MongoDB 5.0 的所有特征。构建 Amazon DocumentDB 5.0 时,我们从客户最迫切要求我们构建的特征和功能逆向工作。我们将根据客户要求我们构建的内容,持续添加其他 MongoDB 5.0 功能。有关受支持 API 的最新列表,请参阅 支持的 MongoDB API、操作和数据类型

Amazon DocumentDB 5.0 入门

了解 Amazon DocumentDB 5.0 入门,请参阅入门指南。您可以使用 Amazon Web Services Management Console 或 Amazon SDK、Amazon CLI 或 Amazon CloudFormation 创建新的 Amazon Document DB 5.0 集群。连接到 Amazon DocumentDB 时,要求您使用与 MongoDB 5.0 或更高版本兼容的 MongoDB 驱动程序或实用工具。

注意

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

Amazon DocumentDB 5.0 支持 Amazon EC2 Graviton2 处理器,如您集群的r6gt4.medium 实例类型,并且可用于所有受支持区域。有关定价的更多信息,请参阅 Amazon DocumentDB(与 MongoDB 兼容)定价

升级或迁移到 Amazon DocumentDB 4.0

您可以使用 Amazon DMS 或实用工具如 mongodumpmongorestoremongoimportmongoexport 从 MongoDB 3.6 或 MongoDB 4.0 迁移 到 Amazon DocumentDB 5.0。有关如何迁移的说明,请参阅 使用 Amazon Database Migration Service 升级您的 Amazon DocumentDB 集群

功能差异

Amazon DocumentDB 4.0 和 5.0 之间的功能差异

随着 Amazon DocumentDB 5.0 发布,Amazon DocumentDB 3.6 和Amazon DocumentDB 4.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 中的“$”投影返回带所有字段的所有文档。在 Amazon DocumentDB 5.0 中,带“$”投影的 find 命令返回匹配查询参数的文档,这种参数仅含有匹配“$”投影的字段。

  • 在 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 兼容性

Amazon DocumentDB 4.0 特征

Amazon DocumentDB 4.0 引入了许多新特征和新功能,包括 ACID 事务和对变更流的改进。以下摘要显示了 Amazon DocumentDB 4.0 中引入的一些主要特征。要查看功能的完整列表,请参阅 发布说明

  • ACID 事务:Amazon DocumentDB 现在支持跨多个文档、报表、集合和数据库执行事务的能力。事务通过使您能够在 Amazon DocumentDB 集群内部跨一个或多个文档执行原子操作、一致操作、隔离操作和持久操作 (ACID),简化应用程序开发。有关更多信息,请参阅事务

  • 更改流:您现在有能力在集群层面(client.watch()mongo.watch())和在数据库(db.watch())打开更改流,可以指定 startAtOperationTime 来打开更改流游标,并且最后,您现在可以将更改流保留期延长至 7 天(之前为 24 小时)。有关更多信息,请参阅教程:将变更流与 Amazon DocumentDB 结合使用

  • Amazon Database Migration Service(Amazon DMS):现在,您可以使用 Amazon DMS 迁移您的 MongoDB 4.0 工作负载至 Amazon DocumentDB。Amazon DMS 现在支持 MongoDB 4.0 源、Amazon DocumentDB 4.0 目标和 Amazon DocumentDB 3.6 源,用于执行 Amazon DocumentDB 3.6 与 4.0 之间的升级。有关更多信息,请参阅 Amazon DMS 文档

  • 性能和索引化:现在,您可以利用带 $lookup 的索引,查找查询借助含一个字段或一个字段且 _id 字段可以从索引中直接提供而无需从集合(涵盖的查询)中读取,借助 findAndModify 进行 hint() 的能力、对 $addToSet 的性能优化和缩小索引总体大小的改进。有关更多信息,请参阅发布说明

  • 操作符:Amazon DocumentDB 4.0 现在支持许多新的聚合操作符:$ifNull$replaceRoot$setIsSubset$setInstersection$setUnion$setEquals。您可以在 支持的 MongoDB API、操作和数据类型 查看我们支持的所有 MongoDB API、操作和数据类型。

  • 基于角色的访问控制 (RBAC):同时使用命令 ListCollectionListDatabase ,您现在可以选择使用 authorizedCollectionsauthorizedDatabases 参数允许用户列出他们有权访问的集合和数据库,而无需分别使用 listCollectionslistDatabase 角色。您还可以杀死自己的游标,而无需使用 KillCursor 角色。

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

Amazon DocumentDB 4.0 入门

了解 Amazon DocumentDB 4.0 入门,请参阅入门指南。您可以使用 Amazon Web Services Management Console 或 Amazon SDK、Amazon CLI 或 Amazon CloudFormation 创建新的 Amazon Document DB 4.0 集群。连接到 Amazon DocumentDB 时,要求您使用与 MongoDB 4.0 或更高版本兼容的 MongoDB 驱动程序或实用工具。

注意

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

Amazon DocumentDB 4.0 支持您集群的 r5r6gt3.mediumt4g.medium 实例类型,并且可用于所有受支持区域。没有使用 Amazon DocumentDB 4.0 的额外成本。有关定价的更多信息,请参阅 Amazon DocumentDB(与 MongoDB 兼容)定价

升级或迁移到 Amazon DocumentDB 4.0

您可以使用 Amazon DMS 或实用工具如 mongodumpmongorestoremongoimportmongoexport 从 MongoDB 3.6 或 MongoDB 4.0 迁移到 Amazon DocumentDB 4.0。类似地,您可以使用相同工具从 Amazon DocumentDB 3.6 升级到 Amazon DocumentDB 4.0。有关如何迁移的说明,请参阅 使用 Amazon Database Migration Service 升级您的 Amazon DocumentDB 集群

功能差异

Amazon DocumentDB 3.6 与 4.0 之间的功能差异

随着 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”。

  • minKeymaxKey的行为:在 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 之间的功能差异

以下是 Amazon DocumentDB 4.0 与 MongoDB 4.0 之间的功能差异。

  • 在路径中借助空键查找:当集合包含数组内部带空键的文档(例如{"x" : [ { "" : 10 }, { "b" : 20 } ]}),且查询中所用的密钥以空字符串结尾(例如x.)时,则 Amazon DocumentDB 将返回该文档,因为其遍历该数组中的所有文档,而 MongoDB 将不返回该文档。

  • 路径中的 $setOnInsert$ :在 Amazon DocumentDB 中字段操作符 $setOnInsert 将与 $ 在路径中共同发挥作用,这也与 MongoDB 4.0 一致。