Synopsis简介
deleteA当操作从集合中删除文档时,例如当用户或应用程序执行deleteevent occurs when operations remove documents from a collection, such as when a user or application executes thedeletecommand.delete命令时,会发生delete事件。
Description描述
_id |
| |
clusterTime | Timestamp |
|
collectionUUID | UUID |
UUID
|
documentKey |
| |
lsid |
| |
ns |
| |
ns.coll |
| |
ns.db |
| |
operationDescription |
| |
operationType |
| |
txnNumber | NumberLong |
|
wallTime | ISODate |
|
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使用changeStreamPreAndPostImagesfield for a collection usingdb.createCollection(),create, orcollMod.db.createCollection()、create或collMod为集合启用新的changeStreamPreAndPostImages字段。Set在fullDocumentBeforeChangeto"required"or"whenAvailable"indb.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以下示例将整个集群上的expireAfterSecondsto100seconds on an entire cluster:expireAfterSeconds设置为100秒:use admin
db.runCommand( {
setClusterParameter:
{ changeStreamOptions: {
preAndPostImages: { expireAfterSeconds: 100 }
} }
} )The following example returns the current以下示例返回当前changeStreamOptionssettings, includingexpireAfterSeconds:changeStreamOptions设置,包括expireAfterSeconds: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.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如果从oplog中删除了更改流事件,则相应的预映像和后映像也将被删除,而不管映像保留时间为expireAfterSecondspre- and post-image retention time.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将文档大小限制为8 MB。如果其他更改流事件字段(如updateDescriptionare not large.updateDescription)不大,您可以在更改流输出中同时请求预映像和后映像。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.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不要引起replacechange event. Areplaceevent always includes the post-image.replace事件。replace事件总是包括帖子图像。
To request a pre-image, you set要请求预映像,您可以在fullDocumentBeforeChangetorequiredorwhenAvailableindb.collection.watch(). To request a post-image, you setfullDocumentusing the same method.db.collection.watch()中将fullDocumentBeforeChange设置为必需或可用。要请求前映像,您可以使用相同的方法设置fullDocument。Pre-images are written to the预映像会写入config.system.preimagescollection.config.system.preimages集合。Theconfig.system.preimagescollection may become large. To limit the collection size, you can setexpireAfterSecondstime 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 从MongoDB 6.0开始,如果您将文档预映像和后映像用于更改流,则必须使用collMod command before you can downgrade to an earlier MongoDB version.collMod命令为每个集合禁用changeStreamPreAndPostImages,然后才能降级到早期的MongoDB版本。
Tip
For change stream events and output, see Change Events.有关更改流事件和输出,请参阅更改事件。To watch a collection for changes, see要查看集合的更改,请参阅db.collection.watch().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: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。