replace
Event事件
On this page本页内容
Summary摘要
replace
-
A当更新操作从集合中删除文档并将其替换为新文档时,会发生replace
event occurs when an update operation removes a document from a collection and replaces it with a new document, such as when thereplaceOne
method is called.replace
事件,例如调用replaceOne
方法时。
Description描述
_id | Document | resumeToken for the resumeAfter parameter when resuming a change stream. resumeAfter 参数的resumeToken 。_id object has the following form: _id 对象具有以下形式:{ _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)。_data types._data 类型的完整列表。resumeToken , see Resume a Change Stream. resumeToken 恢复更改流的示例,请参阅恢复更改流。 |
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 的组合。 |
collectionUUID | UUID | UUID |
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 字段已经是分片键的一部分,则该字段不会重复。 |
fullDocument | document | changeStreamPreAndPostImages option using db.createCollection() , create , or collMod , then the fullDocument field shows the document after it was inserted, replaced, or updated (the document post-image). db.createCollection() 、create 或collMod 设置changeStreamPreAndPostImages 选项,则fullDocument 字段将显示插入、替换或更新后的文档(文档后映像)。fullDocument is always included for insert events. insert 事件总是包含fullDocument 。 |
fullDocumentBeforeChange | document | changeStreamPreAndPostImages field for a collection using db.createCollection() method or the create or collMod commands. db.createCollection() 方法或create 或collMod 命令为集合启用changeStreamPreAndPostImages 字段时,此字段可用。 |
lsid | document | |
ns | document | |
ns.coll | string | |
ns.db | string | |
operationType | string | replace for these change events. replace 。 |
txnNumber | NumberLong | lsid 一起,是一个有助于唯一识别事务的数字。 |
wallTime | ISODate | wallTime differs from clusterTime in that clusterTime is a timestamp taken from the oplog entry associated with the database operation event. wallTime 与clusterTime 的不同之处在于,clusterTime 是从与数据库操作事件关联的oplog条目中获取的时间戳。 |
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:DocumentBeforeChange
文档,其中包含更改(或删除)文档之前的字段:
Enable the new使用changeStreamPreAndPostImages
field for a collection usingdb.createCollection()
,create
, orcollMod
.db.createCollection()
、create
或collMod
为集合启用新的changeStreamPreAndPostImages
字段。Set在fullDocumentBeforeChange
to"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以下示例将expireAfterSeconds
to100
seconds:expireAfterSeconds
设置为100
秒:use admin
db.runCommand( {
setClusterParameter:
{ changeStreamOptions: { preAndPostImages: { expireAfterSeconds: 100 } } }
} )The following example returns the current以下示例返回当前changeStreamOptions
settings, includingexpireAfterSeconds
:changeStreamOptions
设置,包括expireAfterSeconds
: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.expireAfterSeconds
设置为off
使用默认的保留策略:保留前映像和后映像,直到从操作日志中删除相应的更改流事件。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.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 megabytes. To limit the event size, you can:将更改流事件大小限制为小于16兆字节。要限制事件大小,您可以:Limit the document size to 8 megabytes.将文档大小限制为8兆字节。You can request pre- and post-images simultaneously in the change stream output if other change stream event fields like如果其他变更流事件字段(如updateDescription
are not large.updateDescription
)不大,则可以在变更流输出中同时请求前图像和后图像。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.updateDescription
)不太大,则请求在更改流输出中为高达16兆字节的文档发布图像。Request only pre-images in the change stream output for documents up to 16 megabytes if:如果出现以下情况,则仅在更改流输出中请求高达16兆字节的文档的预图像: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.replace
更改事件。replace
事件始终包括发布图像。
To request a pre-image, you set若要请求前映像,请在fullDocumentBeforeChange
torequired
orwhenAvailable
indb.collection.watch()
.db.collection.watch()
中将fullDocumentBeforeChange
设置为required
或whenAvailable
。To request a post-image, you set若要请求发布图像,请使用相同的方法设置fullDocument
using the same method.fullDocument
。Pre-images are written to the预映像将写入config.system.preimages
collection.config.system.preimages
集合。Theconfig.system.preimages
collection may become large.config.system.preimages
集合可能会变大。To limit the collection size, you can set要限制集合大小,您可以为预图像设置expireAfterSeconds时间,如前所示。expireAfterSeconds
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 从MongoDB 6.0开始,如果您对更改流使用文档前和后图像,则必须使用collMod
command before you can downgrade to an earlier MongoDB version.collMod
命令为每个集合禁用changeStreamPreAndPostImages
,然后才能降级到早期的MongoDB版本。
See also: 另请参阅:
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.有关更改流输出的完整示例,请参阅文档前后图像的更改流。
Examples实例
The following example illustrates a 以下示例说明了replace
event:replace
事件:
{
"_id": { <Resume Token> },
"operationType": "replace",
"clusterTime": <Timestamp>,
"wallTime": <ISODate>,
"ns": {
"db": "engineering",
"coll": "users"
},
"documentKey": {
"_id": ObjectId("599af247bb69cd89961c986d")
},
"fullDocument": {
"_id": ObjectId("599af247bb69cd89961c986d"),
"userName": "alice123",
"name": "Alice"
}
}
A replace
operation uses the update command, and consists of two stages:replace
操作使用update
命令,并由两个阶段组成:
Delete the original document with the使用documentKey
anddocumentKey
删除源文档,并且Insert the new document using the same使用相同的documentKey
documentKey
插入新文档
The fullDocument
of a replace
event represents the document after the insert of the replacement document.replace
事件的fullDocument
表示插入替换文档后的文档。