Change Events更改事件
On this page本页内容
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 | true . showExpandedEvents 选项设置为true 。 |
createIndexes | true . showExpandedEvents 选项设置为true 。 |
delete | |
drop | |
dropDatabase | |
dropIndexes | true . showExpandedEvents 选项设置为true 。 |
insert | |
invalidate | |
modify | true . showExpandedEvents 选项设置为true 。 |
refineCollectionShardKey | |
rename | |
replace | |
reshardCollection | |
shardCollection | true . showExpandedEvents 选项设置为true 。 |
update |
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.
Starting in MongoDB 6.0, change streams support change notifications for DDL events, like the createIndexes and dropIndexes events. 从MongoDB 6.0开始,更改流支持DDL事件的更改通知,如createIndexes
和dropIndexes
事件。To include expanded events in a change stream, create the change stream cursor using the 要在变更流中包括展开的事件,请使用showExpandedEvents
option.showExpandedEvents
选项创建变更流游标。
For example:例如:
let cur = db.names.aggregate( [ {
$changeStream: {
showExpandedEvents: true
}
}
] )
cur.next()