View a markdown version of this page

$change StreamSplitLargeEvent - Amazon DocumentDB

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

$change StreamSplitLargeEvent

弹性集群不支持此聚合阶段。

此阶段从亚马逊文档数据库引擎版本 8.0.2 开始可用。

$changeStreamSplitLargeEvent聚合阶段在 $ChangeStream 管道中用于将超过 16 MB 的事件拆分为较小的片段。它通过变更流光标按顺序返回每个片段。

参数

无。$changeStreamSplitLargeEvent舞台的输入应为空文档。

示例 — MongoDB 外壳

以下示例演示使用$changeStreamSplitLargeEvent舞台将超大变更事件拆分为多个片段。

查询示例

// Insert a document db.inventory.insertOne({ _id: 1, item: "Widget", payload: "x".repeat(9 * 1024 * 1024) }) // Open change stream with the $changeStreamSplitLargeEvent stage var changeStream = db.inventory.aggregate([ { $changeStream: { fullDocument: "updateLookup" } }, { $changeStreamSplitLargeEvent: {} } ]); // Update the document db.inventory.updateOne({ _id: 1 }, { $set: { payload: "y".repeat(9 * 1024 * 1024) } }) // Read the change event if (changeStream.hasNext()) { print(tojson(changeStream.next())); }

输出

{ _id: { _data: '...' }, splitEvent: { fragment: 1, of: 2}, ns: { db: 'test', coll: 'inventory' }, clusterTime: Timestamp(4, 1789154779), documentKey: { _id: 1 }, fullDocument: { _id: 1, item: 'Widget', payload: yyyyyyyy...(9437184 chars) }, operationType: 'update', } { _id: { _data: '...' }, splitEvent: { fragment: 2, of: 2}, updateDescription: { updatedFields: { payload: yyyyyyyy...(9437184 chars) }, removedFields: [] } }

代码示例

要查看使用$changeStreamSplitLargeEvent聚合阶段的代码示例,请选择要使用的语言的选项卡:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('inventory'); // Insert a large document first await collection.insertOne({ _id: 1, item: 'Widget', payload: 'x'.repeat(9 * 1024 * 1024) }); // Open change stream with $changeStreamSplitLargeEvent const changeStream = collection.watch( [{ $changeStreamSplitLargeEvent: {} }], { fullDocument: 'updateLookup' } ); changeStream.on('change', (change) => { console.log('Change detected:', change); }); // Update to trigger a split event — fullDocument + updateDescription > 16 MB setTimeout(async () => { console.log('Triggering update...'); await collection.updateOne({ _id: 1 }, { $set: { payload: 'y'.repeat(9 * 1024 * 1024) } }); }, 1000); // Keep connection open to receive changes // In production, handle cleanup appropriately } example();
Python
from pymongo import MongoClient import threading import time def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] collection = db['inventory'] # Insert large document first collection.drop() collection.insert_one({'_id': 1, 'item': 'Widget', 'payload': 'x' * (9 * 1024 * 1024)}) # Open change stream with $changeStreamSplitLargeEvent change_stream = collection.watch( [{'$changeStreamSplitLargeEvent': {}}], full_document='updateLookup' ) # Update in separate thread after delay def update_doc(): collection.update_one({'_id': 1}, {'$set': {'payload': 'y' * (9 * 1024 * 1024)}}) threading.Thread(target=update_doc).start() # Watch for changes for change in change_stream: print('Change detected:', change) client.close() example()