

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon DocumentDB 與 MongoDB 的相容性
<a name="compatibility"></a>

Amazon 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**
+ [Amazon DocumentDB 8.0 中的新功能](#compatibility-whatsnew-8)
+ [開始使用 Amazon DocumentDB](#compatibility-getstarted-5)
+ [升級或遷移至 Amazon DocumentDB 5.0 或 8.0](#compatibility-upgrade-5)
+ [功能差異](#compatibility-differences-5)

### Amazon DocumentDB 8.0 中的新功能
<a name="compatibility-whatsnew-8"></a>

Amazon DocumentDB 8.0 可將查詢效能提升高達 7 倍，並將壓縮率提升高達 5 倍，讓您能夠以較低的成本建置高效能應用程式。Amazon DocumentDB 8.0 具有與 MongoDB 8.0 的完整線路通訊協定相容性。以下摘要介紹了 Amazon DocumentDB 8.0 中介紹的一些主要功能。若要查看新功能的完整清單，請參閱 [版本備註](release-notes.md)。
+ 透過新增對 MongoDB 8.0 API 驅動程式的支援，提供與 MongoDB 8.0 的相容性。也支援使用 MongoDB API 6.0 和 7.0 版建置的應用程式。
+ Amazon DocumentDB 8.0 中的新查詢規劃器將效能改進擴展到彙總階段運算子，以及支援彙總管道最佳化和不同的命令。
+ Amazon DocumentDB 8.0 支援定序和檢視。
+ 提供 6 個新的彙總階段：\$1replaceWith、\$1vectorSearch、\$1merge、\$1set、\$1unset、\$1bucket，以及 3 個新的彙總運算子 \$1pow、\$1rand、\$1dateTrunc。
+ 新版本的文字索引：Amazon DocumentDB 8.0 中的文字索引 v2 引入了其他字符，增強了文字搜尋功能。
+ 透過平行向量索引建置，Amazon DocumentDB 8.0 可將索引建置時間縮短高達 30 倍。

### 開始使用 Amazon DocumentDB 8.0
<a name="compatibility-getstarted-5"></a>

若要開始使用 Amazon DocumentDB 8.0，請參閱 [入門指南](https://docs.aws.amazon.com/documentdb/latest/developerguide/get-started-guide.html)。您可以使用 AWS 管理主控台 或 SDK AWS CLI或 建立新的 Amazon DocumentDB 8 AWS .0 叢集 CloudFormation。連線至 Amazon DocumentDB 時，您必須使用與 MongoDB 5.0 或更高版本相容的 MongoDB 驅動程式或公用程式。

**注意**  
使用 AWS SDK AWS CLI或 時 CloudFormation，引擎版本預設為 5.0.0。您必須明確指定 參數，`engineVersion = 8.0.0`以建立新的 Amazon DocumentDB 8.0 叢集`engineVersion = 4.0.0`，或建立新的 Amazon DocumentDB 4.0 叢集`engineVersion = 3.6.0`，或建立新的 Amazon DocumentDB 3.6 叢集。對於指定的 Amazon DocumentDB 叢集，您可以使用 來決定叢集版本 AWS CLI ，以呼叫`describe-db-clusters`或使用 Amazon DocumentDB 管理主控台來檢視特定叢集的引擎版本編號。

Amazon DocumentDB 5.0 和 8.0 支援 Amazon EC2 `r8g` Graviton4 處理器和 Graviton2 處理器，例如叢集的 `r6g`和 `t4.medium`執行個體類型，且在所有支援的區域中皆可使用 （請參閱 [執行個體](what-is.md#what-is-db-instances))。如需定價的詳細資訊，請參閱 [Amazon DocumentDB （與 MongoDB 相容） 定價](https://aws.amazon.com/documentdb/pricing/)。

### 升級或遷移至 Amazon DocumentDB 5.0 或 8.0
<a name="compatibility-upgrade-5"></a>

您可以使用 或 公用程式，例如 [`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 DMS](https://aws.amazon.com/dms/)或 Amazon DocumentDB 8.0。如需如何遷移的指示，請參閱 [使用 升級 Amazon DocumentDB 叢集 AWS Database Migration Service](docdb-migration.versions.md)。

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

#### Amazon DocumentDB 5.0 和 8.0 之間的功能差異
<a name="w2aac11b9c11b3"></a>

隨著 Amazon DocumentDB 8.0 的發行，Amazon DocumentDB 5.0 和 Amazon DocumentDB 8.0 之間存在功能差異：
+  規劃器 v1 是 Amazon DocumentDB 5.0 中的預設查詢規劃器，而效能更高的規劃器 v3 是 Amazon DocumentDB 8.0 中的預設值。
+  Amazon DocumentDB 8.0 中的新功能，包括檢視、定序和類似 \$1merge 的運算子，僅與規劃器 v3 相容。
+  在 Amazon DocumentDB 8.0 中，壓縮預設為開啟，並設定為使用 Zstandard 演算法。此外，在 Amazon 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)
+ [升級或遷移至 Amazon 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 叢集和碎片型彈性叢集，將儲存限制提高至 128 TiB。
+ 推出 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 DocumentDB 中支援的 MongoDB APIs、操作和資料類型](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 管理主控台 或 SDK AWS CLI或 建立新的 Amazon DocumentDB 5 AWS .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 叢集，您可以使用 來決定叢集版本 AWS CLI ，以呼叫`describe-db-clusters`或使用 Amazon DocumentDB 管理主控台來檢視特定叢集的引擎版本編號。

Amazon DocumentDB 5.0 和 8.0 支援 Amazon EC2 `r8g` Graviton4 處理器和 Graviton2 處理器，例如叢集的 `r6g`和 `t4.medium`執行個體類型，且在所有支援的區域中皆可使用 （請參閱 [執行個體](what-is.md#what-is-db-instances))。如需定價的詳細資訊，請參閱 [Amazon DocumentDB （與 MongoDB 相容） 定價](https://aws.amazon.com/documentdb/pricing/)。

### 升級或遷移至 Amazon DocumentDB 5.0
<a name="compatibility-upgrade-5"></a>

您可以使用 或 公用程式，例如 [`mongodump`、`mongoimport`、 和 `mongorestore`，`mongoexport`](https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html)從 MongoDB 3.6 [AWS DMS](https://aws.amazon.com/dms/)或 MongoDB 4.0 遷移至 Amazon DocumentDB 5.0。如需如何遷移的指示，請參閱 [使用 升級 Amazon DocumentDB 叢集 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"：
  + 在字串表達式中找不到子字串，或
  + start 是大於 end 的數字，或
  + start 是大於字串位元組長度的數字。
+ 在 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)：您現在可以使用 AWS DMS 將 MongoDB 4.0 工作負載遷移至 Amazon DocumentDB。 AWS DMS 現在支援 MongoDB 4.0 來源、Amazon DocumentDB 4.0 目標和 Amazon DocumentDB 3.6 來源，以在 Amazon DocumentDB 3.6 和 4.0 之間執行升級。如需詳細資訊，請參閱 [AWS DMS 文件](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html)。
+ **效能和索引**：您現在可以使用具有 的索引`$lookup`、尋找具有包含一個欄位或一個欄位的投影的查詢，並且可以直接從索引提供 `_id` 欄位，而無需從集合讀取 （涵蓋的查詢）、`hint()`具有 的功能`findAndModify`、 的效能最佳化`$addToSet`，以及減少整體索引大小的改進。如需詳細資訊，請參閱[版本備註](release-notes.md)。
+ **運算子**：Amazon DocumentDB 4.0 現在支援許多新的彙總運算子：`$ifNull`、`$replaceRoot`、`$setIsSubset`、`$setIntersection``$setUnion`、、`$setEquals`。您可以在 中查看我們支援的所有 MongoDB APIs、操作和資料類型[Amazon DocumentDB 中支援的 MongoDB APIs、操作和資料類型](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 DocumentDB 中支援的 MongoDB APIs、操作和資料類型](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)。您可以使用 AWS 管理主控台 或 SDK AWS CLI或 建立新的 Amazon DocumentDB 4 AWS .0 叢集 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 叢集，您可以使用 來決定叢集版本 AWS CLI ，以呼叫`describe-db-clusters`或使用 Amazon DocumentDB 管理主控台來檢視特定叢集的引擎版本編號。

Amazon DocumentDB 4.0 支援叢集的 `r5`、`t3.medium`、 `r6g`和 `t4g.medium`執行個體類型，並可在所有支援的區域中使用。使用 Amazon DocumentDB 4.0 無需額外費用。如需定價的詳細資訊，請參閱 [Amazon DocumentDB （與 MongoDB 相容） 定價](https://aws.amazon.com/documentdb/pricing/)。

### 升級或遷移至 Amazon DocumentDB 4.0
<a name="compatibility-upgrade"></a>

您可以使用 或 公用程式，例如 [`mongodump`、`mongoimport`、 和 `mongorestore`，`mongoexport`](https://docs.aws.amazon.com/documentdb/latest/developerguide/backup_restore-dump_restore_import_export_data.html)從 MongoDB 3.6 [AWS DMS](https://aws.amazon.com/dms/)或 MongoDB 4.0 遷移至 Amazon DocumentDB 4.0。同樣地，您可以使用相同的工具從 Amazon DocumentDB 3.6 升級到 Amazon DocumentDB 4.0。如需如何遷移的指示，請參閱 [使用 升級 Amazon DocumentDB 叢集 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`，則兩個版本中的行為都相同。不過，如果投影是 Amazon DocumentDB 3.6`{a:{b:{c:1}}}`，則只會將投影套用至 'a'，而不是 'b' 或 'c'。
+ **、 `minKey`的行為`maxKey`**：在 Amazon DocumentDB 4.0 中， 的行為不會`{x:{$gt:MaxKey}}`傳回任何內容，而 則`{x:{$lt:MaxKey}}`傳回所有內容。
+ **文件比較差異**：比較子文件中不同類型 （雙、整數、長） 的數值 （例如，在 `b`中`{"_id" :1, "a" :{"b":1}}`) 現在提供跨數值資料類型和文件每個層級的一致輸出。

#### 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` 路徑`$`中的 與** ： 欄位運算子`$setOnInsert`不會在 Amazon DocumentDB 的 路徑`$`中與 搭配使用，這也與 MongoDB 4.0 一致。