Docs Menu
Docs Home
/
MongoDB Manual
/ /

replace Event

On this page

  • Summary
  • Description
  • Behavior
  • Document Pre- and Post-Images
  • Examples
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 the replaceOne method is called.

Field
Type
Description
_id
Document

A BSON object which serves as an identifier for the change stream event. This value is used as the resumeToken for the resumeAfter parameter when resuming a change stream. The _id object has the following form:

{
"_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. See Resume Tokens for the full list of _data types.

For an example of resuming a change stream by resumeToken, see Resume a Change Stream.

clusterTime
Timestamp

The timestamp from the oplog entry associated with the event.

Change stream event notifications associated with a multi-document transaction all have the same clusterTime value: the time when the transaction was committed.

Events with the same clusterTime may not all relate to the same transaction. Some events don't relate to a transaction at all. Starting in MongoDB 8.0, this may be true for events on any deployment. In previous versions, this behavior was possible only for events on a sharded cluster.

To identify events for a single transaction, you can use the combination of lsid and txnNumber in the change stream event document.

Changed in version 8.0.

collectionUUID
UUID

UUID identifying the collection where the change occurred.

New in version 6.0.

documentKey
document

Document that contains the _id value of the document created or modified by the CRUD operation.

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.

fullDocument
document

The new document created by the operation.

Changed in version 6.0.

Starting in MongoDB 6.0, if you set the 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). fullDocument is always included for insert events.

fullDocumentBeforeChange
document

The document before changes were applied by the operation. That is, the document pre-image.

This field is available when you enable the changeStreamPreAndPostImages field for a collection using db.createCollection() method or the create or collMod commands.

New in version 6.0.

lsid
document

The identifier for the session associated with the transaction.

Only present if the operation is part of a multi-document transaction.

ns
document

The namespace (database and or collection) affected by the event.

ns.coll
string

The name of the collection where the event occurred.

ns.db
string

The name of the database where the event occurred.

operationType
string

The type of operation that the change notification reports.

Returns a value of replace for these change events.

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

wallTime

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

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:

  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 on an entire cluster:

      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:

The following example illustrates a replace event:

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

  • Delete the original document with the documentKey and

  • Insert the new document using the same documentKey

The fullDocument of a replace event represents the document after the insert of the replacement document.

Back

rename