Change streams watch collections, databases, or deployments for changes.更改流监视集合、数据库或部署的更改。
When a change occurs on a watched resource, the change stream returns a change event notification document, with information on the operation and the changes it made.当监视的资源发生更改时,更改流会返回一个更改事件通知文档,其中包含有关操作及其所做更改的信息。
Operation Types操作类型
create |
|
createIndexes |
|
delete | |
drop | |
dropDatabase | |
dropIndexes |
|
insert | |
invalidate | |
modify |
|
refineCollectionShardKey |
|
rename | |
replace | |
reshardCollection |
|
shardCollection |
|
update |
Note
The server might internally process and return update operations as replace operations if the representation of replace operations are more concise. If you are listening for update operations, we strongly recommend also listening for replace operations.如果替换操作的表示更简洁,服务器可能会在内部处理更新操作并将其作为替换操作返回。如果您正在监听更新操作,我们强烈建议您也监听替换操作。
Resume Token恢复令牌
Each change event includes an 每个更改事件都包含一个_id field, which contain a document. _id字段,其中包含一个文档。This document can serve as the resume token when starting a change stream.此文档可以在启动更改流时用作恢复令牌。
Expanded Events扩展活动
New in version 6.0.在版本6.0中新增。
Starting in MongoDB 6.0, change streams support change notifications for DDL events, like the createIndexes and dropIndexes events. To include expanded events in a change stream, create the change stream cursor using the 从MongoDB 6.0开始,更改流支持DDL事件的更改通知,如showExpandedEvents option.createIndexes和dropIndexes事件。要在更改流中包含扩展事件,请使用showExpandedEvents选项创建更改流游标。
For example:例如:
let cur = db.names.aggregate( [ {
$changeStream: {
showExpandedEvents: true
}
}
] )
cur.next()