Change Events改变事件

On this page本页内容

Change Events改变事件

The following document represents all possible fields that a change stream response document can have.以下文档表示变更流响应文档可能具有的所有字段。

{
   _id : { <BSON Object> },
   "operationType" : "<operation>",
   "fullDocument" : { <document> },
   "ns" : {
      "db" : "<database>",
      "coll" : "<collection>"
   },
   "to" : {
      "db" : "<database>",
      "coll" : "<collection>"
   },
   "documentKey" : { "_id" : <value> },
   "updateDescription" : {
      "updatedFields" : { <document> },
      "removedFields" : [ "<field>", ... ]
   }
   "clusterTime" : <Timestamp>,
   "txnNumber" : <NumberLong>,
   "lsid" : {
      "id" : <UUID>,
      "uid" : <BinData>
   }
}

Some fields are only available for certain operations, such as updates. 某些字段仅适用于某些操作,例如更新。The following table describes each field in the change stream response document:下表描述了变更流响应文档中的每个字段:

Field字段Type类型Description描述
_id document

Metadata related to the operation. 与操作相关的元数据。Acts as the resumeToken for the resumeAfter parameter when resuming a change stream.在恢复更改流时,充当resumeAfter参数的resumeToken

{
   "_data" : <BinData|hex string>
}

The _data type depends on the MongoDB versions and, in some cases, the feature compatibility version (fcv) at the time of the change stream’s opening/resumption. _data类型取决于MongoDB版本,在某些情况下,还取决于更改流打开/恢复时的功能兼容性版本(fcv)。For details, see Resume Tokens.有关详细信息,请参阅恢复令牌

operationType string一串

The type of operation that occurred. Can be any of the following values:发生的操作类型。可以是以下任意值:

  • insert
  • delete
  • replace
  • update
  • drop
  • rename
  • dropDatabase
  • invalidate
fullDocument document

The document created or modified by the insert, replace, delete, update operations (i.e. CRUD operations).通过insertreplacedeleteupdate操作(即CRUD操作)创建或修改的文档。

For insert and replace operations, this represents the new document created by the operation.对于insertreplace操作,这表示该操作创建的新文档。

For delete operations, this field is omitted as the document no longer exists.对于delete操作,此字段将被忽略,因为文档已不存在。

For update operations, this field only appears if you configured the change stream with fullDocument set to updateLookup. 对于更新操作,仅当将fullDocument设置为updateLookup时,此字段才会出现。This field then represents the most current majority-committed version of the document modified by the update operation. 然后,此字段表示更新操作修改的文档的最新多数提交版本。This document may differ from the changes described in updateDescription if other majority-committed operations modified the document between the original update operation and the full document lookup.如果其他多数提交操作在原始更新操作和完整文档查找之间修改了文档,则此文档可能与updateDescription中描述的更改不同。

ns document The namespace (database and or collection) affected by the event.受事件影响的命名空间(数据库和/或集合)。
ns.db string The name of the database.数据库的名称。
ns.coll string

The name of the collection.集合的名称。

For dropDatabase operations, this field is omitted.对于dropDatabase操作,省略此字段。

to document When operationType : rename, this document displays the new name for the ns collection. operationType : rename时,此文档显示ns集合的新名称。This document is omitted for all other values of operationType.对于operationType的所有其他值,省略本文档。
to.db string The new name of the database.数据库的新名称。
to.coll string The new name of the collection.集合的新名称。
documentKey document A document that contains the _id of the document created or modified by the insert, replace, delete, update operations (i.e. CRUD operations). 包含由insertreplacedeleteupdate操作(即CRUD操作)创建或修改的文档的_id的文档。For sharded collections, also displays the full shard key for the document. 对于分片集合,还显示文档的完整分片键。The _id field is not repeated if it is already a part of the shard key.如果_id字段已经是碎片密钥的一部分,则不会重复该字段。
updateDescription document

A document describing the fields that were updated or removed by the update operation.描述由更新操作更新或删除的字段的文档。

This document and its fields only appears if the operationType is update.仅当operationTypeupdate时,才会显示此文档及其字段。

updateDescription.updatedFields document A document whose keys correspond to the fields that were modified by the update operation. 其键与更新操作修改的字段相对应的文档。The value of each field corresponds to the new value of those fields, rather than the operation that resulted in the new value.每个字段的值对应于这些字段的新值,而不是生成新值的操作。
updateDescription.removedFields array An array of fields that were removed by the update operation.更新操作删除的字段数组。
clusterTime Timestamp

The timestamp from the oplog entry associated with the event.与事件关联的oplog条目的时间戳。

For events that happened as part of a multi-document transaction, the associated change stream notifications will have the same clusterTime value, namely the time when the transaction was committed.对于作为多文档事务一部分发生的事件,关联的更改流通知将具有相同的clusterTime值,即提交事务的时间。

On a sharded cluster, events that occur on different shards can have the same clusterTime but be associated with different transactions or even not be associcated with any transaction. 在分片集群上,发生在不同分片上的事件可以具有相同的clusterTime,但与不同的事务关联,甚至不与任何事务关联。To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document.要识别单个事务的事件,可以在变更流事件文档中使用lsidtxnNumber的组合。

New in version 4.0.版本4.0中的新功能。

txnNumber NumberLong

The transaction number.交易号。

Only present if the operation is part of a multi-document transaction.仅当操作是多文档事务的一部分时才显示。

New in version 4.0.版本4.0中的新功能。

lsid Document

The identifier for the session associated with the transaction.与事务关联的会话的标识符。

Only present if the operation is part of a multi-document transaction.仅当操作是多文档事务的一部分时才显示。

New in version 4.0.版本4.0中的新功能。

insert Event

The following example illustrates an insert event:以下示例演示了一个insert事件:

{
   _id: { < Resume Token > },
   operationType: 'insert',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      userName: 'alice123',
      _id: ObjectId("599af247bb69cd89961c986d")
   },
   fullDocument: {
      _id: ObjectId("599af247bb69cd89961c986d"),
      userName: 'alice123',
      name: 'Alice'
   }
}

The documentKey field includes both the _id and the userName field. documentKey字段包括_iduserName字段。This indicates that the engineering.users collection is sharded, with a shard key on userName and _id.这表明engineering.users集合是分片的,userName_id上有一个分片键。

The fullDocument document represents the version of the document at the time of the insert.fullDocument文档表示插入时文档的版本。

update Event事件

The following example illustrates an update event:以下示例演示了一个update事件:

{
   _id: { < Resume Token > },
   operationType: 'update',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      _id: ObjectId("58a4eb4a30c75625e00d2820")
   },
   updateDescription: {
      updatedFields: {
         email: 'alice@10gen.com'
      },
      removedFields: ['phoneNumber']
   }
}

The following example illustrates an update event for change streams opened with the fullDocument : updateLookup option:以下示例演示了使用fullDocument:updateLookup选项打开的更改流的update事件:

{
   _id: { < Resume Token > },
   operationType: 'update',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      _id: ObjectId("58a4eb4a30c75625e00d2820")
   },
   updateDescription: {
      updatedFields: {
         email: 'alice@10gen.com'
      },
      removedFields: ['phoneNumber']
   },
   fullDocument: {
      _id: ObjectId("58a4eb4a30c75625e00d2820"),
      name: 'Alice',
      userName: 'alice123',
      email: 'alice@10gen.com',
      team: 'replication'
   }
}

The fullDocument document represents the most current majority-committed version of the updated document. fullDocument文档代表更新文档的最新多数提交版本。The fullDocument document may vary from the document at the time of the update operation depending on the number of interleaving majority-committed operations that occur between the update operation and the document lookup.fullDocument文档可能与更新操作时的文档不同,具体取决于更新操作和文档查找之间发生的交错多数提交操作的数量。

replace Event事件

The following example illustrates a replace event:以下示例演示了一个replace事件:

{
   _id: { < Resume Token > },
   operationType: 'replace',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      _id: ObjectId("599af247bb69cd89961c986d")
   },
   fullDocument: {
      _id: ObjectId("599af247bb69cd89961c986d"),
      userName: 'alice123',
      name: 'Alice'
   }
}

A replace operation uses the update command, and consists of two stages:replace操作使用update命令,由两个阶段组成:

The fullDocument of a replace event represents the document after the insert of the replacement document.replace事件的fullDocument表示插入替换文档后的文档。

delete Event

The following example illustrates a delete event:以下示例演示了一个delete事件:

{
   _id: { < Resume Token > },
   operationType: 'delete',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   documentKey: {
      _id: ObjectId("599af247bb69cd89961c986d")
   }
}

The fullDocument document is omitted as the document no longer exists at the time the change stream cursor sends the delete event to the client.当变更流游标向客户机发送delete事件时,由于文档不再存在,fullDocument文档被省略。

drop Event事件

New in version 4.0.1.版本4.0.1中新增。

A drop event occurs when a collection is dropped from a database. 从数据库中删除集合时会发生drop事件。The following example illustrates a drop event:以下示例演示了一个drop事件:

{
   _id: { < Resume Token > },
   operationType: 'drop',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   }
}

A drop event leads to an invalidate event for change streams opened against its ns collection.drop事件导致针对其ns集合打开的更改流发生invalidate事件

rename Event事件

New in version 4.0.1.版本4.0.1中新增。

A rename event occurs when a collection is renamed. rename集合时会发生重命名事件。The following example illustrates a rename event:以下示例演示了rename事件:

{
   _id: { < Resume Token > },
   operationType: 'rename',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering',
      coll: 'users'
   },
   to: {
      db: 'engineering',
      coll: 'people'
   }
}

A rename event leads to an invalidate event for change streams opened against its ns collection or to collection.rename事件会导致针对其ns集合或to集合打开的更改流的invalidate事件

dropDatabase Event事件

New in version 4.0.1.版本4.0.1中新增。

A dropDatabase event occurs when a database is dropped. 删除数据库时会发生dropDatabase事件。The following example illustrates a dropDatabase event:以下示例演示了dropDatabase事件:

{
   _id: { < Resume Token > },
   operationType: 'dropDatabase',
   clusterTime: <Timestamp>,
   ns: {
      db: 'engineering'
   }
}

A dropDatabase command generates a drop event for each collection in the database before generating a dropDatabase event for the database.在为数据库生成dropDatabase事件之前,dropDatabase命令会为数据库中的每个集合生成drop事件

A dropDatabase event leads to an invalidate event for change streams opened against its ns.db database.dropDatabase事件会导致针对其ns.db数据库打开的更改流发生invalidate事件

invalidate Event事件

The following example illustrates an invalidate event:以下示例演示了一个invalidate事件:

{
   _id: { < Resume Token > },
   operationType: 'invalidate',
   clusterTime: <Timestamp>
}

For change streams opened up against a collection, a drop event, rename event, or dropDatabase event that affects the watched collection leads to an invalidate event.对于针对集合打开的更改流,影响监视的集合的drop事件rename事件dropDatabase事件会导致失效事件。

For change streams opened up against a database, a dropDatabase event that affects the watched database leads to an invalidate event.对于针对数据库打开的更改流,影响监视的数据库的dropDatabase事件会导致失效事件。

invalidate events close the change stream cursor.invalidate事件关闭更改流光标。

You cannot use resumeAfter to resume a change stream after an invalidate event (for example, a collection drop or rename) closes the stream. invalidate事件(例如,集合删除或重命名)关闭更改流后,不能使用resumeAfter恢复更改流。Starting in MongoDB 4.2, you can use startAfter to start a new change stream after an invalidate event.从MongoDB 4.2开始,可以使用startAfterinvalidate事件后启动新的更改流。