$changeStream

$changeStream集計ステージでは、データの変更をリアルタイムで追跡する変更ストリーム カーソルが開きます。 このステージにより、アプリケーションはコレクション内で発生する挿入、更新、削除、およびその他の操作に対応できます。

構文

{
  $changeStream: {
    allChangesForCluster: <boolean>,
    fullDocument: <string>,
    fullDocumentBeforeChange: <string>,
    resumeAfter: <ResumeToken>,
    startAfter: <ResumeToken>,
    startAtOperationTime: <Timestamp>,
    showExpandedEvents: <boolean>
  }
}

パラメーター

パラメーター Description
allChangesForCluster Optional. ブール値。 true の場合は、クラスター全体の変更を返します。 既定値は false です。
fullDocument Optional. String. 更新操作で返される内容を決定します。 オプション: 'default'、'updateLookup'、'whenAvailable'、'required'。
fullDocumentBeforeChange Optional. String. ドキュメントの事前イメージを返します。 オプション: "off"、"whenAvailable"、"required"。
resumeAfter Optional. 特定のイベントの後に変更ストリームを再開するトークンを再開します。
startAfter Optional. トークンを再開して、特定のイベントの後に変更ストリームを開始します。
startAtOperationTime Optional. 特定の時刻から変更ストリームを開始するためのタイムスタンプ。
showExpandedEvents Optional. ブール値。 別の変更ストリーム イベントを含めます。 既定値は false です。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

例 1: ストア コレクション内のすべての変更を監視する

この操作は、ストア コレクション内のすべての変更を監視する変更ストリームを設定します。

db.stores.aggregate([
  {
    $changeStream: {
      fullDocument: "updateLookup"
    }
  }
])

ストア ドキュメントが更新されると、変更ストリームは完全なドキュメントを含む変更イベントを返します。

{
  "_id": { "_data": "AeARBpQ/AAAA" },
  "operationType": "update",
  "fullDocument": {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "sales": {
      "revenue": 42500
    },
    "company": "Trey Research",
    "lastUpdated": "ISODate('2024-06-16T10:30:00.000Z')"
  },
  "ns": {
    "db": "StoreData",
    "coll": "stores"
  },
  "documentKey": {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac"
  }
}