Synopsis
deleteA
deleteevent occurs when operations remove documents from a collection, such as when a user or application executes thedeletecommand.
Description
| Field | Type | Description |
|---|---|---|
| Document | A BSON object which serves as an identifier for the change stream event. This value is used as the The For an example of resuming a change stream by |
| Timestamp |
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 Events with the same To identify events for a single transaction, you can use the combination of Changed in version 8.0. |
| UUID | UUID identifying the collection where the change occurred. New in version 6.0. The |
| document | Document that contains the For sharded collections, this field also displays the full shard key for the document. The |
| document | The identifier for the session associated with the transaction. Only present if the operation is part of a multi-document transaction. |
| document | The namespace (database and or collection) affected by the event. |
| string | The name of the collection where the event occurred. |
| string | The name of the database where the event occurred. |
| document | 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. |
| string | The type of operation that the change notification reports. Returns a value of |
| NumberLong | Together with the lsid, a number that helps uniquely identify a transction. Only present if the operation is part of a multi-document transaction. |
| The server date and time of the database operation. 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:
- Enable the new
changeStreamPreAndPostImagesfield for a collection usingdb.createCollection(),create, orcollMod. - Set
fullDocumentBeforeChangeto"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
expireAfterSecondsto100seconds on an entire cluster:use admin
db.runCommand( {
setClusterParameter:
{ changeStreamOptions: {
preAndPostImages: { expireAfterSeconds: 100 }
} }
} )The following example returns the current
changeStreamOptionssettings, includingexpireAfterSeconds:db.adminCommand( { getClusterParameter: "changeStreamOptions" } )- Setting
expireAfterSecondstooffuses 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
expireAfterSecondspre- 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 mebibytes. 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
updateDescriptionare not large. - Request only post-images in the change stream output for documents up to 16 mebibytes if other change stream event fields like
updateDescriptionare not large. Request only pre-images in the change stream output for documents up to 16 mebibytes if:
- document updates affect only a small fraction of the document structure or content, and
- do not cause a
replacechange event. Areplaceevent always includes the post-image.
- 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
- To request a pre-image, you set
fullDocumentBeforeChangetorequiredorwhenAvailableindb.collection.watch(). To request a post-image, you setfullDocumentusing the same method. Pre-images are written to the
config.system.preimagescollection.The
config.system.preimagescollection may become large. To limit the collection size, you can setexpireAfterSecondstime 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
- 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.