$changeStream

匯總 $changeStream 階段會開啟變更數據流數據指標,以即時追蹤數據變更。 此階段可讓應用程式回應集合中發生的插入、更新、刪除和其他作業。

語法

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

參數

參數 Description
allChangesForCluster 選擇性。 布林值。 如果為 true,則會傳回整個叢集的變更。 預設值為 false。
fullDocument 選擇性。 String. 決定要針對更新作業傳回的內容。 選項:『default』、『updateLookup』、『whenAvailable』、『required』。
fullDocumentBeforeChange 選擇性。 String. 傳回檔的前置圖像。 選項:「off」、“whenAvailable”、“required”。
resumeAfter 選擇性。 繼續令牌,以在特定事件之後繼續變更數據流。
startAfter 選擇性。 繼續令牌,以在特定事件之後開始變更數據流。
startAtOperationTime 選擇性。 從特定時間開始變更數據流的時間戳。
showExpandedEvents 選擇性。 布林值。 包含另一個變更數據流事件。 預設值為 false。

範例

請參考商店集合中的此範例檔。

{
    "_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"
  }
}