delete Event
Synopsis提要
Description描述
_id | Document | resumeToken for the resumeAfter parameter when resuming a change stream. resumeAfter 参数的resumeToken 。_id object has the following form:_id 对象具有以下形式:
{
|
clusterTime | Timestamp | clusterTime value: the time when the transaction was committed.clusterTime 值:事务提交的时间。clusterTime may not all relate to the same transaction. clusterTime 的事件可能并不都与同一事务相关。lsid and txnNumber in the change stream event document. lsid 和txnNumber 的组合。New in version 4.0. |
collectionUUID | UUID | UUID New in version 6.0. |
documentKey | document | _id value of the document created or modified by the CRUD operation._id 值的文档。_id field is not repeated if it is already a part of the shard key. _id 字段已经是shard密钥的一部分,则该字段不会重复。 |
lsid | document | New in version 4.0. |
ns | document | |
ns.coll | string | |
ns.db | string | |
operationDescription | document | New in version 6.0. |
operationType | string | delete for these change events. delete 值。 |
txnNumber | NumberLong | lsid 一起,是一个有助于唯一识别事务的数字。New in version 4.0. |
wallTime | ISODate | 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 从MongoDB 6.0开始,如果执行以下步骤,您将看到一个fullDocumentBeforeChange
document with the fields before the document was changed (or deleted) if you perform these steps:fullDocumentBeforeChange
文档,其中包含更改(或删除)文档之前的字段:
-
Enable the new
changeStreamPreAndPostImages
field for a collection usingdb.createCollection()
,create
, orcollMod
. -
Set
fullDocumentBeforeChange
to"required"
or"whenAvailable"
indb.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
to100
seconds:use admin
db.runCommand( {
setClusterParameter:
{ changeStreamOptions: { preAndPostImages: { expireAfterSeconds: 100 } } }
} ) -
The following example returns the current
changeStreamOptions
settings, includingexpireAfterSeconds
:db.adminCommand( { getClusterParameter: "changeStreamOptions" } )
-
Setting
expireAfterSeconds
tooff
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. Areplace
event always includes the post-image.
-
-
-
To request a pre-image, you set
fullDocumentBeforeChange
torequired
orwhenAvailable
indb.collection.watch()
. To request a post-image, you setfullDocument
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 setexpireAfterSeconds
time for the pre-images as shown earlier. -
Pre-images are removed asynchronously by a background process.
-
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.
See also:
-
For change stream events and output, see Change Events.
-
To watch a collection for changes, see
db.collection.watch()
. -
For complete examples with the change stream output, see Change Streams with Document Pre- and Post-Images.
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.