Docs HomeMongoDB Manual

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描述
_idDocumentA 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 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恢复更改流的示例,请参阅恢复更改流

clusterTimeTimestampThe timestamp from the oplog entry associated with the event.与事件关联的oplog条目中的时间戳。
Change stream event notifications associated with a multi-document transaction all have the same clusterTime value: the time when the transaction was committed.多文档事务关联的更改流事件通知都具有相同的clusterTime值:事务提交的时间。
On sharded clusters, events with the same clusterTime may not all relate to the same transaction. 在分片集群上,具有相同clusterTime的事件可能并不都与同一事务相关。Some events don't relate to a transaction at all.有些事件根本与事务无关。
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.
collectionUUIDUUIDUUID identifying the collection where the change occurred. 标识发生更改的集合。
New in version 6.0.
documentKeydocumentDocument 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字段已经是shard密钥的一部分,则该字段不会重复。
lsiddocumentThe 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.
nsdocumentThe namespace (database and or collection) affected by the event. 受事件影响的命名空间(数据库和/或集合)。
ns.collstringThe name of the collection where the event occurred. 发生事件的集合的名称。
ns.dbstringThe name of the database where the event occurred. 发生事件的数据库的名称。
operationDescriptiondocumentAdditional information on the change operation.有关更改操作的其他信息。
This document and its subfields only appears when the change stream uses expanded events. 只有当变更流使用展开的事件时,才会显示此文档及其子字段。
New in version 6.0.
operationTypestringThe type of operation that the change notification reports.更改通知报告的操作类型。
Returns a value of delete for these change events. 返回这些更改事件的delete值。
txnNumberNumberLongTogether with the lsid, a number that helps uniquely identify a transction.lsid一起,是一个有助于唯一识别事务的数字。
Only present if the operation is part of a multi-document transaction. 仅当操作是多文档事务的一部分时才显示。
New in version 4.0.
wallTimeISODateThe 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.
New in version 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.

  2. Set fullDocumentBeforeChange to "required" or "whenAvailable" in db.collection.watch().

Example fullDocumentBeforeChange document in the change stream output:

"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.

    • The following example sets expireAfterSeconds to 100 seconds:

      use admin
      db.runCommand( {
      setClusterParameter:
      { changeStreamOptions: { preAndPostImages: { expireAfterSeconds: 100 } } }
      } )
    • The following example returns the current changeStreamOptions settings, including 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.

    • 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.

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 megabytes. To limit the event size, you can:

    • 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.

    • Request only post-images in the change stream output for documents up to 16 megabytes if other change stream event fields like updateDescription are not large.

    • Request only pre-images in the change stream output for documents up to 16 megabytes if:

      • 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.

  • 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.

  • Pre-images are written to the config.system.preimages collection.

    • 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.

    • 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.

Tip

See also:

Example实例

The following example illustrates a delete event:

{
"_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.