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>", ... ],
      "truncatedArrays" : [
         { "field" : <field>, "newSize" : <integer> },
         ...
      ]
   },
   "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描述
_iddocument

A BSON object which serves as an identifier for the change stream event. BSON对象,用作变更流事件的标识符。This value is used as the resumeToken for the resumeAfter parameter when resuming a change stream. 恢复更改流时,此值用作resumeAfter参数的resumeTokenThe _id object has the following form:_id对象具有以下格式:

{
   "_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)
See Resume Tokens for the full list of _data types.有关数据类型的完整列表,请参阅恢复令牌

See Resume a Change Stream for an example of resuming a change stream by resumeToken.有关通过resumeToken恢复更改流的示例,请参阅恢复更改流

operationTypestring

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

  • insert
  • delete
  • replace
  • update
  • drop
  • rename
  • dropDatabase
  • invalidate
fullDocumentdocument

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. 对于update操作,只有将更改流配置为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中描述的更改不同。

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

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

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

todocumentWhen 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.dbstringThe new name of the database.数据库的新名称。
to.collstringThe new name of the collection.集合的新名称。
documentKeydocumentA 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字段已经是分片键的一部分,则不会重复该字段。
updateDescriptiondocument

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
documentA 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
arrayAn array of fields that were removed by the update operation.更新操作删除的字段数组。
updateDescription.
truncatedArrays
array

An array of documents which record array truncations performed with pipeline-based updates using one or more of the following stages:记录使用以下一个或多个阶段进行的基于管道的更新所执行的数组截断的文档数组:

Note注意

If the entire array is replaced, the truncations will be reported under updateDescription.updatedFields.如果替换整个数组,截断将在updateDescription.updatedFields下报告。

updateDescription.
truncatedArrays.
field
stringThe name of the truncated field.截断字段的名称。
updateDescription.
truncatedArrays.
newSize
integerThe number of elements in the truncated array.截断数组中的元素数。
clusterTimeTimestamp

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中新增

txnNumberNumberLong

The transaction number.事务记录编号。

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

New in version 4.0.在版本4.0中新增

lsidDocument

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.这表明engineeringusers集合是分片的,在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'],
      truncatedArrays: [ {
         "field" : "vacation_time",
         "newSize" : 36
      } ]
   }
}

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'],
      truncatedArrays: [ {
         "field" : "vacation_time",
         "newSize" : 36
      } ]
   },
   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操作使用更新命令,由两个阶段组成:

  • Delete the original document with the documentKey and使用documentKey
  • Insert the new document using the same documentkey使用相同的documentkey插入新文档

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

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.当变更流游标向客户机发送删除事件时,由于文档不再存在,因此省略了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集合打开的更改流的失效事件

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集合或到集合打开的更改流的无效事件

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命令为数据库中的每个集合生成一个drop事件,然后再为数据库生成drop数据库事件。

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

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.事件关闭变更流游标。

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

←  Change Streams Production RecommendationsReplication →