AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

複数コレクションの最初の同期の設定

initialSync lets an Atlas Stream Processing stream processor ingest preexisting documents in an Atlas collection as though they were insert changeEvent documents before it starts tailing the change stream. A single $source stage can now run initialSync against more than one collection at once.

このページでは、単一コレクションと複数コレクション同期の特性を比較し、複数コレクション同期の構成手順を説明し、その上に構築されたクロスクラスターレプリケーションパイプラインを示します。

The coll field of the $source stage accepts either a single collection name or an array of collection names. Every collection that you name in the array must belong to the database that you specify in the db field. You can't synchronize collections from more than one database in a single $source stage. The value you provide determines the sync scope:

スコープ
$source 構成
動作

単一コレクション

db: "app", coll: "orders"

Atlas Stream Processing は、コレクション という名前の 1 つのコレクションを同期します。

Explicit list

db: "app", coll: ["a", "b", "c"]

Atlas Stream Processing は、リスト内の各コレクションを一覧表示の順序で同期します。

A multi-collection initialSync operation has the following traits:

  • Syncs collections in list order. With coll: ["a", "b", "c"], Atlas Stream Processing drains a's partitions before b's, and b's before c's, except where spillover fills idle capacity with partitions from a later collection.

  • 完了したコレクションを再同期せずに再開します。 Atlas Stream Processing はどのコレクションが終了したかを追跡するため、再起動されたストリーム プロセッサは完了したコレクションをスキップし、進行中のコレクションを最後に同期されたドキュメントから再開します。すでに出力したドキュメントが再配信される可能性があるため、繰り返されるドキュメントを冪等で処理するようにシンクを設計します。

  • コレクションレベルの詳細な同期の進行状況を公開します。 stats().stats.operatorStats[0].targetStats reports sync progress and document counts for each target collection. To learn more, see Verify Initial Sync Progress.

Multi-collection initialSync differs from single-collection sync in ways that affect how you configure and operate your stream processor:

  • Parallelism is global, not per collection. Setting initialSync.parallelism caps the number of concurrent partition reads across the entire set of target collections, not per collection.

  • キャッチアップ スコープは、ソース スコープの後にします。同期フェーズの後、Atlas Stream Processing は、ソースのスコープに一致する変更ストリームを開きます。たとえば、データベース全体のソースの場合はデータベースレベルの変更ストリーム、クラスター全体のソースの場合はクラスターレベルの変更ストリーム、またはフィルタリングされた変更ストリームはソースのスコープに一致します。コレクションの明示的なリスト。これを個別に構成する必要はありません。

  • コレクションの同期に失敗すると、プロセッサ全体が失敗します。単一コレクション同期の場合と同様に、Atlas Stream Processing がリスト内のいずれかのコレクションの同期に失敗すると、ストリーム プロセッサ全体が失敗します。

  • 一部のソース側構成は宛先に複製されません。 initialSync では、インデックス、Cappedコレクション、時系列コレクション、バリデーター、デフォルトの照合、ビュー定義などのコレクションオプションは複製されません。ストリーム プロセッサを起動する前に、宛先でこれらを作成します。 Atlas Stream Processing は、ストリーム プロセッサの起動後に作成したコレクションも検出しません。同期するコレクションのセットは、その点で 固定されています 。

The following procedure uses two collections from the sample_analytics dataset: customers, which holds customer profiles, and accounts, which holds each customer's financial accounts. The procedure configures a stream processor that copies both collections from one Atlas cluster to another, then keeps them current as documents change.

この手順を完了する前に、以下の手順を行ってください。

このセクションのコマンドをクラスターに対して実行します。接続するには、「mongosh を使用してクラスターに接続する」を参照してください。

ソース コレクションを準備するには、次の手順を実行します。

1

This procedure uses the customers and accounts collections from the sample_analytics dataset. To learn how to load sample data, see Import Sample Data Into Your Atlas Deployment.

2

Enable pre- and post-images on the customers and accounts collections, because the procedure's $source stage sets fullDocument: "required". Run the following commands against the cluster:

db.getSiblingDB("sample_analytics").runCommand({
collMod: "customers",
changeStreamPreAndPostImages: { enabled: true }
})
db.getSiblingDB("sample_analytics").runCommand({
collMod: "accounts",
changeStreamPreAndPostImages: { enabled: true }
})

To learn more about fullDocument: "required", see MongoDB Collection Change Stream.

Select the Atlas UI or mongosh to configure the stream processor.

After you start the stream processor, call sp.<processor-name>.stats() with the verbose option to view sync progress for each target collection. The source operator's entry in stats.operatorStats reports a targetStats array with one element per collection:

sp.replicate_analytics_sp.stats(
{ options: { verbose: true } }
).stats.operatorStats[0].targetStats

Each target's initialSync.status field cycles through pending, in_progress, and completed as Atlas Stream Processing syncs that collection, or reports failed if the sync fails. estimatedDocs and copiedDocs report the estimated and completed document counts for that one collection.

To view total progress across every target collection, use the source operator's own initialSync field, which sums estimatedDocs and copiedDocs across all collections:

sp.replicate_analytics_sp.stats(
{ options: { verbose: true } }
).stats.operatorStats[0].initialSync

The preceding procedure replicates two collections between Atlas clusters. The following example applies the same pipeline to a different set of collections. Only the db and coll values in the $source stage change.

マルチコレクションの最初の同期と 動的名前空間ルーティング を組み合わせて、コレクション セットを複製します。

The pipeline synchronizes existing documents in orders and customers, then continues to replicate changes to both collections as they occur. The aggregation has three stages:

  1. The $source stage synchronizes orders and customers on <source-connection-name>, then opens a change stream scoped to those two collections.

  2. The $replaceRoot stage selects the document key for delete events and the full document for every other event. Because metadata isn't part of the document body, stream.source.* metadata survives this stage.

  3. The $merge stage writes each event to the matching collection on <destination-connection-name>, using the source event's namespace metadata to route the write and the operation type to choose between inserting, replacing, or deleting the destination document.

sp = db.createStreamProcessor("replicate-app-db", [
{ $source: {
connectionName: "<source-connection-name>",
db: "app",
coll: ["orders", "customers"],
config: { fullDocument: "required" },
initialSync: { enable: true }
} },
{ $replaceRoot: {
newRoot: {
$cond: {
if: { $eq: [{ $meta: "stream.source.operationType" }, "delete"] },
then: { $meta: "stream.source.documentKey" },
else: "$fullDocument"
}
}
} },
{ $merge: {
into: {
connectionName: "<destination-connection-name>",
db: { $meta: "stream.source.ns.db" },
coll: { $meta: "stream.source.ns.coll" }
},
on: "_id",
whenMatched: { $cond: {
if: { $eq: [{ $meta: "stream.source.operationType" }, "delete"] },
then: "delete",
else: "replace"
} },
whenNotMatched: { $cond: {
if: { $eq: [{ $meta: "stream.source.operationType" }, "delete"] },
then: "discard",
else: "insert"
} }
} }
]);
sp.start();

Before you start this stream processor, create any indexes, collection options, or views that orders and customers need on <destination-connection-name>. initialSync doesn't replicate these.

このガイドのステージと概念についてさらに学ぶには、次のリソースを参照してください。