Database Manual / Change Streams

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描述
create

Occurs on the creation of a collection.在创建集合时发生。

Requires that you set the showExpandedEvents option to true.要求将showExpandedEvents选项设置为true

New in version 6.0.在版本6.0中新增。

createIndexes

Occurs 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.从数据库中删除集合时发生。
dropDatabaseOccurs when a database is dropped.删除数据库时发生。
dropIndexes

Occurs 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.当操作使更改流无效时发生。
modify

Occurs when a collection is modified.在修改集合时发生。

Requires that you set the showExpandedEvents option to true.要求将showExpandedEvents选项设置为true

New in version 6.0.在版本6.0中新增。

refineCollectionShardKey

Occurs when a shard key is modified.在修改分片键时发生。

New in version 6.1.在版本6.1中新增。

renameOccurs when a collection is renamed.重命名集合时发生。
replaceOccurs when an update operation removes a document from a collection and replaces it with a new document.当更新操作从集合中删除文档并用新文档替换它时发生。
reshardCollection

Occurs when the shard key for a collection and the distribution of data changes.当集合的分片键和数据分布发生变化时发生。

Requires that you set the showExpandedEvents option to true.要求将showExpandedEvents选项设置为true

New in version 6.1.在版本6.1中新增。 (Also available in 6.0.146.0.14中也有提供)

shardCollection

Occurs 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.当操作更新集合中的文档时发生。

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 showExpandedEvents option.从MongoDB 6.0开始,更改流支持DDL事件的更改通知,如createIndexesdropIndexes事件。要在更改流中包含扩展事件,请使用showExpandedEvents选项创建更改流游标。

For example:例如:

let cur = db.names.aggregate( [ {
$changeStream: {
showExpandedEvents: true
}
}
] )

cur.next()