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

変更イベントをフィルタリングするためのパイプラインのカスタマイズ

この使用例では、MongoDB Kafka ソース コネクタが消費するデータをカスタマイズするようにパイプラインを構成する方法を示します。 パイプライン は、データをフィルタリングまたは変換するためのデータベースへの指示で構成される MongoDB 集計パイプラインです。

MongoDB notifies the connector of data changes that match your aggregation pipeline on a change stream. A change stream is a sequence of events that describe data changes a client made to a MongoDB deployment in real-time. For more information, see the MongoDB server manual entry on Change Streams.

イベントを調整していて、特定のイベントでの各ゲストの名前と訪問時間を収集したいとします。 ゲストがイベントにチェックインするたびに、アプリケーションは次の詳細を含む新しいドキュメントを挿入します。

{
"_id": ObjectId(...),
"eventId": 321,
"name": "Dorothy Gale",
"arrivalTime": 2021-10-31T20:30:00.245Z
}

connector pipeline設定を定義して、変更ストリームに変更イベント情報をフィルタリングするように指示できます。

  • 挿入操作の変更イベントを作成し、他のすべてのタイプの操作のイベントを省略します。

  • fullDocument.eventIdの値「321」と一致するドキュメントのみの変更イベントを作成し、他のすべてのドキュメントを省略します。

  • _ideventIdfullDocumentプロジェクションを使用して、 オブジェクトから フィールドと フィールドを省略します。

これらの変換を適用するには、次の集計パイプラインをpipeline設定に割り当てます。

pipeline=[{"$match": { "$and": [{"operationType": "insert"}, { "fullDocument.eventId": 321 }] } }, {"$project": { "fullDocument._id": 0, "fullDocument.eventId": 0 } } ]

重要

Make sure that the results of the pipeline contain the top-level _id and ns fields of the payload object. MongoDB uses id as the value of the resume token, and ns to generate the Kafka output topic name.

アプリケーションがサンプル ドキュメントを挿入すると、構成されたコネクターは次のレコードを Kafka トピックに公開します。

{
...
"payload": {
_id: { _data: ... },
"operationType": "insert",
"fullDocument": {
"name": "Dorothy Gale",
"arrivalTime": "2021-10-31T20:30:00.245Z",
},
"ns": { ... },
"documentKey": {
_id: {"$oid": ... }
}
}
}

ソース コネクタを使用して変更ストリームを管理する方法の詳細については、 Change Streams に関するコネクタのドキュメントを参照してください。

このページを評価