Database Manual / Change Streams / Change Events

delete Event事件

Synopsis简介

delete
A delete event occurs when operations remove documents from a collection, such as when a user or application executes the delete command.当操作从集合中删除文档时,例如当用户或应用程序执行delete命令时,会发生delete事件。

Description描述

Field字段Type类型Description描述
_idDocument文档

A BSON object which serves as an identifier for the change stream event. This value is used as the resumeToken for the resumeAfter parameter when resuming a change stream. The _id object has the following form:BSON对象,用作更改流事件的标识符。在恢复更改流时,此值用作resumeAfter参数的resumeToken_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 or resumption. _data类型取决于MongoDB版本,在某些情况下,还取决于更改流打开或恢复时的功能兼容性版本(FCV)See Resume Tokens for the full list of _data types.有关_data类型的完整列表,请参阅恢复令牌

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

clusterTimeTimestamp

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

Due to oplog size limits, multi-document transactions may create multiple oplog entries. In a transaction, change stream events staged in a given oplog entry share the same clusterTime.由于oplog大小的限制多文档事务可能会创建多个oplog条目。在事务中,给定oplog条目的变更流事件共享相同的clusterTime

Events with the same clusterTime may not all relate to the same transaction. Some events don't relate to a transaction at all. Starting in MongoDB 8.0, this may be true for events on any deployment. In previous versions, this behavior was possible only for events on a sharded cluster.具有相同clusterTime的事件可能并不都与同一事务相关。有些事件根本与事务无关。从MongoDB 8.0开始,这可能适用于任何部署上的事件。在以前的版本中,这种行为仅适用于分片集群上的事件。

To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document.要识别单个事务的事件,可以在更改流事件文档中使用lsidtxnNumber的组合。

Changed in version 8.0.在版本8.0中的更改。

collectionUUIDUUID

UUID identifying the collection where the change occurred.识别发生更改的集合。

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

The collectionUUID field is only included if the change stream is created with the showExpandedEvents field set to true. For details, see showExpandedEvents.只有在创建更改流时将showExpandedEvents字段设置为true时,才会包含collectionUUID字段。有关详细信息,请参阅showExpandedEvents

documentKeydocument文档

Document that contains the _id value of the document created or modified by the CRUD operation.包含通过CRUD操作创建或修改的文档的_id值的文档。

For sharded collections, this field 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字段已经是分片键的一部分,则不会重复。

lsiddocument文档

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

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

nsdocument文档

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

ns.collstring字符串

The name of the collection where the event occurred.发生事件的集合的名称。

ns.dbstring字符串

The name of the database where the event occurred.发生事件的数据库的名称。

operationDescriptiondocument文档

Additional information on the change operation.有关更改操作的其他信息。

This document and its subfields only appears when the change stream uses expanded events.此文档及其子字段仅在更改流使用扩展事件时显示。

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

operationTypestring字符串

The type of operation that the change notification reports.更改通知报告的操作类型。

Returns a value of delete for these change events.为这些更改事件返回delete值。

txnNumberNumberLong

Together with the lsid, a number that helps uniquely identify a transction.lsid一起,一个有助于唯一标识事务的数字。

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

wallTimeISODate

The server date and time of the database operation. 数据库操作的服务器日期和时间。wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event.wallTimeclusterTime的不同之处在于,clusterTime是从与数据库操作事件关联的oplog条目中获取的时间戳。

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

Behavior行为

Document Pre- and Post-Images文档前映和后映像

Starting in MongoDB 6.0, you see a fullDocumentBeforeChange document with the fields before the document was changed (or deleted) if you perform these steps:从MongoDB 6.0开始,如果执行以下步骤,您将看到一个fullDocumentBeforeChange文档,其中包含更改(或删除)文档之前的字段:

  1. Enable the new changeStreamPreAndPostImages field for a collection using db.createCollection(), create, or collMod.使用db.createCollection()createcollMod为集合启用新的changeStreamPreAndPostImages字段。
  2. Set fullDocumentBeforeChange to "required" or "whenAvailable" in db.collection.watch().db.collection.watch()中将fullDocumentBeforeChange设置为"required""whenAvailable"

Example fullDocumentBeforeChange document in the change stream output:更改流输出中的示例fullDocumentBeforeChange文档:

"fullDocumentBeforeChange" : {
"_id" : ObjectId("599af247bb69cd89961c986d"),
"userName" : "alice123",
"name" : "Alice Smith"
}

For complete examples with the change stream output, see Change Streams with Document Pre- and Post-Images.有关更改流输出的完整示例,请参阅使用文档前后图像更改流

Pre- and post-images are not available for a change stream event if the images were:如果图像是以下情况,则前映象和后映象对更改流事件不可用:

  • Not enabled on the collection at the time of a document update or delete operation.在文档更新或删除操作时,集合上未启用。
  • Removed after the pre- and post-image retention time set in expireAfterSeconds.expireAfterSeconds中设置的图像保留时间前后删除。

    • The following example sets expireAfterSeconds to 100 seconds on an entire cluster:以下示例将整个集群上的expireAfterSeconds设置为100秒:

      use admin
      db.runCommand( {
      setClusterParameter:
      { changeStreamOptions: {
      preAndPostImages: { expireAfterSeconds: 100 }
      } }
      } )
    • The following example returns the current changeStreamOptions settings, including expireAfterSeconds:以下示例返回当前changeStreamOptions设置,包括expireAfterSeconds

      db.adminCommand( { getClusterParameter: "changeStreamOptions" } )
    • Setting expireAfterSeconds to off uses the default retention policy: pre- and post-images are retained until the corresponding change stream events are removed from the oplog.expireAfterSeconds设置为off将使用默认保留策略:保留前后图像,直到从oplog中删除相应的更改流事件。
    • If a change stream event is removed from the oplog, then the corresponding pre- and post-images are also deleted regardless of the expireAfterSeconds pre- and post-image retention time.如果从oplog中删除了更改流事件,则相应的预映像和后映像也将被删除,而不管映像保留时间为expireAfterSeconds之前和之后。

Additional considerations:其他注意事项:

  • Enabling pre- and post-images consumes storage space and adds processing time. Only enable pre- and post-images if you need them.启用预映像和后映像会消耗存储空间并增加处理时间。仅在需要时启用前置和后置图像。
  • Limit the change stream event size to less than 16 mebibytes. To limit the event size, you can:将更改流事件大小限制为小于16兆字节。要限制事件大小,您可以:

    • Limit the document size to 8 megabytes. You can request pre- and post-images simultaneously in the change stream output if other change stream event fields like updateDescription are not large.将文档大小限制为8 MB。如果其他更改流事件字段(如updateDescription)不大,您可以在更改流输出中同时请求预映像和后映像。
    • Request only post-images in the change stream output for documents up to 16 mebibytes if other change stream event fields like updateDescription are not large.如果其他更改流事件字段(如updateDescription)不大,则仅请求更改流输出中最多16兆字节的文档的发布图像。
    • Request only pre-images in the change stream output for documents up to 16 mebibytes if:在以下情况下,仅在更改流输出中请求最多16兆字节的文档的预图像:

      • document updates affect only a small fraction of the document structure or content, and文档更新仅影响文档结构或内容的一小部分,以及
      • do not cause a replace change event. A replace event always includes the post-image.不要引起replace事件。replace事件总是包括帖子图像。
  • To request a pre-image, you set fullDocumentBeforeChange to required or whenAvailable in db.collection.watch(). To request a post-image, you set fullDocument using the same method.要请求预映像,您可以在db.collection.watch()中将fullDocumentBeforeChange设置为必需或可用。要请求前映像,您可以使用相同的方法设置fullDocument
  • Pre-images are written to the config.system.preimages collection.预映像会写入config.system.preimages集合。

    • The config.system.preimages collection may become large. To limit the collection size, you can set expireAfterSeconds time for the pre-images as shown earlier.config.system.preimages集合可能会变大。要限制集合大小,您可以为预映像设置expireAfterSeconds时间,如前所示。
    • Pre-images are removed asynchronously by a background process.前图像由后台进程异步删除。

Important

Backward-Incompatible Feature向后不兼容功能

Starting in MongoDB 6.0, if you are using document pre- and post-images for change streams, you must disable changeStreamPreAndPostImages for each collection using the collMod command before you can downgrade to an earlier MongoDB version.从MongoDB 6.0开始,如果您将文档预映像和后映像用于更改流,则必须使用collMod命令为每个集合禁用changeStreamPreAndPostImages,然后才能降级到早期的MongoDB版本。

Tip

Example示例

The following example illustrates a delete event:以下示例说明了delete事件:

{
"_id": { <Resume Token> },
"operationType": "delete",
"clusterTime": <Timestamp>,
"wallTime": <ISODate>,
"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