$changeStreamSplitLargeEvent (aggregation)
On this page本页内容
Definition定义
$changeStreamSplitLargeEvent
New in version 7.0. 7.0版新增。
If a change stream has large events that exceed 16 MB, a 如果变更流包含超过16 MB的大型事件,则返回BSONObjectTooLarge
exception is returned. BSONObjectTooLarge
异常。Starting in MongoDB 7.0, you can use a 从MongoDB 7.0开始,您可以使用$changeStreamSplitLargeEvent
stage to split the events into smaller fragments.$changeStreamSplitTargetEvent
阶段将事件拆分为更小的片段。
You should only use 只有在绝对必要的情况下,才应使用$changeStreamSplitLargeEvent
when strictly necessary. For example, if your application requires full document pre- or post-images, and generates large events that exceed 16 MB, use $changeStreamSplitLargeEvent
.$changeStreamSplitTargetEvent
。例如,如果您的应用程序需要完整的文档前映像或后映像,并且生成的大型事件超过16 MB,请使用$changeStreamSplitTargetEvent
。
Before you decide to use 在决定使用$changeStreamSplitLargeEvent
, you should first try to reduce the change event size. $changeStreamSplitTargetEvent
之前,应首先尝试减小更改事件的大小。For example:例如:
Don't request document pre- or post-images unless your application requires them.除非您的应用程序需要,否则不要要求文档前或后图像。This generates在更多情况下,这会生成fullDocument
andfullDocumentBeforeChange
fields in more cases, which are typically the largest objects in a change event.fullDocument
和fullDocumentBeforeChange
字段,它们通常是更改事件中最大的对象。Use a使用$project
stage to include only the fields necessary for your application.$project
阶段仅包括应用程序所需的字段。This reduces the change event size and avoids the additional time to split large events into fragments. This allows more change events to be returned in each batch.这减少了更改事件的大小,并避免了将大型事件拆分为片段的额外时间。这允许在每个批次中返回更多的更改事件。
You can only have one 管道中只能有一个$changeStreamSplitLargeEvent
stage in your pipeline, and it must be the last stage. $changeStreamSplitTargetEvent
阶段,并且它必须是最后一个阶段。You can only use 只能在$changeStreamSplitLargeEvent
in a $changeStream
pipeline.$changeStream
管道中使用$changeStreamSplitTargetEvent
。
$changeStreamSplitLargeEvent
syntax:语法:
{
$changeStreamSplitLargeEvent: {}
}
Behavior行为
$changeStreamSplitLargeEvent
splits events that exceed 16 MB into fragments and returns the fragments sequentially using the change stream cursor.将超过16MB的事件拆分为片段,并使用更改流游标按顺序返回片段。
The fragments are split so that the maximum number of fields are returned in the first fragment. 片段被分割,使得在第一个片段中返回最大数量的字段。This ensures the event context is returned as quickly as possible.这样可以确保事件上下文尽快返回。
When the change event is split, only the size of top-level fields are used. 拆分更改事件时,只使用顶级字段的大小$changeStreamSplitLargeEvent
does not recursively process or split subdocuments. $changeStreamSplitTargetEvent
不递归处理或拆分子文档。For example, if you use a 例如,如果使用$project
stage to create a change event with a single field that is 20 MB in size, the event is not split and the stage returns an error.$project
阶段创建一个具有20 MB大小的单个字段的更改事件,则不会拆分该事件,并且该阶段将返回错误。
Each fragment has a resume token. 每个片段都有一个恢复标记。A stream that is resumed using a fragment's token will either:使用片段的令牌恢复的流将:
Begin a new stream from the subsequent fragment.从后续片段开始一个新的流。Start at the next event if resuming from the final fragment in the sequence.如果从序列中的最后一个片段恢复,则从下一个事件开始。
Each fragment for an event includes a 事件的每个片段都包括一个splitEvent
document:splitEvent
文档:
splitEvent: {
fragment: <int>,
of: <int>
}
The following table describes the fields.下表介绍了这些字段。
fragment | |
of |
Example实例
The example scenario in this section shows the use of 本节中的示例场景显示了对名为$changeStreamSplitLargeEvent
with a new collection named myCollection
.myCollection
的新集合使用$changeStreamSplitTargetEvent
。
Create 创建myCollection
and insert one document with just under 16 MB of data:myCollection
并插入一个数据不到16 MB的文档:
db.myCollection.insertOne(
{ _id: 0, largeField: "a".repeat( 16 * 1024 * 1024 - 1024 ) }
)
largeField
contains the repeated letter 包含重复的字母a
.a
。
Enable changeStreamPreAndPostImages for 为myCollection
, which allows a change stream to retrieve a document as it was before an update (pre-image) and after an update (post-image):myCollection
启用changeStreamPreAndPostImages
,这允许更改流检索更新前(前图像)和更新后(后图像)的文档:
db.runCommand( {
collMod: "myCollection",
changeStreamPreAndPostImages: { enabled: true }
} )
Create a change stream cursor to monitor changes to 使用myCollection
using db.collection.watch()
:db.collection.watch()
创建一个更改流游标来监视对myCollection
的更改:
myChangeStreamCursor = db.myCollection.watch(
[ { $changeStreamSplitLargeEvent: {} } ],
{ fullDocument: "required", fullDocumentBeforeChange: "required" }
)
For the change stream event:对于变更流事件:
fullDocument: "required"
includes the document post-image.包括文档张贴图像。fullDocumentBeforeChange: "required"
includes the document pre-image.包括文档预图像。
For details, see 有关详细信息,请参阅$changeStream
.$changeStream
。
Update the document in 更新myCollection
, which also produces a change stream event with the document pre- and post-images:myCollection
中的文档,它还会生成一个带有文档前图像和后图像的更改流事件:
db.myCollection.updateOne(
{ _id: 0 },
{ $set: { largeField: "b".repeat( 16 * 1024 * 1024 - 1024 ) } }
)
largeField
now contains the repeated letter 现在包含重复的字母b
.b
。
Retrieve the fragments from 使用myChangeStreamCursor
using the next()
method and store the fragments in objects named firstFragment
, secondFragment
, and thirdFragment
:next()
方法从myChangeStreamCursor
中检索片段,并将片段存储在名为firstFragment
、secondFragment
和thirdFragment
的对象中:
const firstFragment = myChangeStreamCursor.next()
const secondFragment = myChangeStreamCursor.next()
const thirdFragment = myChangeStreamCursor.next()
Show 显示firstFragment.splitEvent
:firstFragment.splitEvent
:
firstFragment.splitEvent
Output with the fragment details:输出片段详细信息:
splitEvent: { fragment: 1, of: 3 }
Similarly, 类似地,secondFragment.splitEvent
and thirdFragment.splitEvent
return:secondFragment.splitEvent
和thirdFragment.splitEvent
返回:
splitEvent: { fragment: 2, of: 3 }
splitEvent: { fragment: 3, of: 3 }
To examine the object keys for 要检查firstFragment
:firstFragment
的对象键,请执行以下操作:
Object.keys( firstFragment )
Output:输出:
[
'_id',
'splitEvent',
'wallTime',
'clusterTime',
'operationType',
'documentKey',
'ns',
'fullDocument'
]
To examine the size in bytes for 要检查firstFragment.fullDocument
:firstFragment.fullDocument
的大小(以字节为单位),请执行以下操作:
bsonsize( firstFragment.fullDocument )
Output:输出:
16776223
secondFragment
contains the fullDocumentBeforeChange
pre-image, which is approximately 16 MB in size. secondFragment
包含完整的DocumentBeforeChange
预映像,其大小约为16 MB。The following example shows the object keys for 以下示例显示了secondFragment
:secondFragment
的对象键:
Object.keys( secondFragment )
Output:输出:
[ '_id', 'splitEvent', 'fullDocumentBeforeChange' ]
thirdFragment
contains the updateDescription
field, which is approximately 16 MB in size. The following example shows the object keys for thirdFragment
:thirdFragment
包含updateDescription
字段,该字段的大小约为16MB。以下示例显示了thirdFragment
的对象键:
Object.keys( thirdFragment )
Output:输出:
[ '_id', 'splitEvent', 'updateDescription' ]
For more information about change streams and events, see Change Events.有关更改流和事件的更多信息,请参阅更改事件。