Docs HomeMongoDB Manual

Change Events更改事件

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操作类型

Event事件Description描述
createOccurs on the creation of a collection.在创建集合时发生。
Requires that you set the showExpandedEvents option to true. 要求您将showExpandedEvents选项设置为true
New in version 6.0. 6.0版新增。
createIndexesOccurs on the creation of indexes on the collection.在对集合创建索引时发生。
Requires that you set the showExpandedEvents option to true. 要求您将showExpandedEvents选项设置为true
New in version 6.0. 6.0版新增。
deleteOccurs when a document is removed from the collection.从集合中删除文档时发生。
dropOccurs when a collection is dropped from a database. 从数据库中删除集合时发生。
New in version 4.0.1. 4.0.1版新增。
dropDatabaseOccurs when a database is dropped. 在删除数据库时发生。
New in version 4.0.1. 4.0.1版新增。
dropIndexesOccurs when an index is dropped from the collection.从集合中删除索引时发生。
Requires that you set the showExpandedEvents option to true. 要求您将showExpandedEvents选项设置为true
New in version 6.0. 6.0版新增。
insertOccurs when an operation adds documents to a collection.在操作将文档添加到集合时发生。
invalidateOccurs when an operation renders the change stream invalid.当操作使更改流无效时发生。
modifyOccurs when a collection is modified.在修改集合时发生。
Requires that you set the showExpandedEvents option to true. 要求您将showExpandedEvents选项设置为true
New in version 6.0. 6.0版新增。
refineCollectionShardKeyOccurs when a shard key is modified. 在修改分片键时发生。
New in version 6.1. 6.1版新增。
renameOccurs when a collection is renamed. 在重命名集合时发生。
New in version 4.0.1. 4.0.1版新增。
replaceOccurs when an update operation removes a document from a collection and replaces it with a new document.当更新操作从集合中删除文档并将其替换为新文档时发生。
reshardCollectionOccurs when the shard key for a collection and the distribution of data changes. 当数据的集合和分布的分片键更改时发生。
New in version 6.1. 6.1版新增。
shardCollectionOccurs when a collection is sharded.在对集合进行分片时发生。
Requires that you set the showExpandedEvents option to true. 要求您将showExpandedEvents选项设置为true
New in version 6.0. 6.0版新增。
updateOccurs when an operation updates a document in a collection.当操作更新集合中的文档时发生。

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事件的更改通知,如createIndexesdropIndexes事件。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()