Docs 菜单

变更事件

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.

事件
说明

Occurs on the creation of a collection.

需要将 showExpandedEvents 选项设置为 true

6.0 版本中的新功能

Occurs on the creation of indexes on the collection.

需要将 showExpandedEvents 选项设置为 true

6.0 版本中的新功能

Occurs when a document is removed from the collection.

Occurs when a collection is dropped from a database.

Occurs when a database is dropped.

Occurs when an index is dropped from the collection.

需要将 showExpandedEvents 选项设置为 true

6.0 版本中的新功能

Occurs when an operation adds documents to a collection.

Occurs when an operation renders the change stream invalid.

Occurs when a collection is modified.

需要将 showExpandedEvents 选项设置为 true

6.0 版本中的新功能

Occurs when a shard key is modified.

6.1 版本新增内容

Occurs when a collection is renamed.

Occurs when an update operation removes a document from a collection and replaces it with a new document.

Occurs when the shard key for a collection and the distribution of data changes.

需要将 showExpandedEvents 选项设置为 true

6.1 版本新增: (在 6.0.14 中也可用)

Occurs when a collection is sharded.

需要将 showExpandedEvents 选项设置为 true

6.0 版本中的新功能

Occurs when an operation updates a document in a collection.

Each change event includes an _id field, which contain a document. This document can serve as the resume token when starting a change stream.

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.

例如:

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