Docs Menu
Docs Home
/
MongoDB Manual
/ /

update Event

On this page

  • Summary
  • Description
  • Example
update

An update event occurs when an operation updates a document in a collection.

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

clusterTime is the timestamp from the oplog entry associated with the event.

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

On sharded clusters, events with the same clusterTime may not all relate to the same transaction. Some events don't relate to a transaction at all.

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

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 document created or modified by a CRUD operation.

This field only appears if you configured the change stream with fullDocument set to updateLookup. When you configure the change stream with updateLookup, the field represents the current majority-committed version of the document modified by the update operation. The document may differ from the changes described in updateDescription if any other majority-committed operations have modified the document between the original update operation and the full document lookup.

For more information, see Lookup Full Document for Update Operations.

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 update for these change events.

updateDescription

document

A document describing the fields that were updated or removed by the update operation.

updateDescription.
removedFields

array

An array of fields that were removed by the update operation.

updateDescription.
truncatedArrays

array

An array of documents which record array truncations performed with pipeline-based updates using one or more of the following stages:

If the entire array is replaced, the truncations will be reported under updateDescription.updatedFields.

updateDescription.
truncatedArrays.
field

string

The name of the truncated field.

updateDescription.
truncatedArrays.
newSize

integer

The number of elements in the truncated array.

updateDescription.
updatedFields

document

A document whose keys correspond to the fields that were modified by the update operation. The value of each field corresponds to the new value of those fields, rather than the operation that resulted in the new value.

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.

The following example illustrates an update event:

{
"_id": { <Resume Token> },
"operationType": "update",
"clusterTime": <Timestamp>,
"ns": {
"db": "engineering",
"coll": "users"
},
"documentKey": {
"_id": ObjectId("58a4eb4a30c75625e00d2820")
},
"updateDescription": {
"updatedFields": {
"email": "alice@10gen.com"
},
"removedFields": ["phoneNumber"],
"truncatedArrays": [ {
"field" : "vacation_time",
"newSize" : 36
} ]
}
}

The following example illustrates an update event for change streams opened with the fullDocument : updateLookup option:

{
"_id": { <Resume Token> },
"operationType": "update",
"clusterTime": <Timestamp>,
"ns": {
"db": "engineering",
"coll": "users"
},
"documentKey": {
"_id": ObjectId("58a4eb4a30c75625e00d2820")
},
"updateDescription": {
"updatedFields": {
"email": "alice@10gen.com"
},
"removedFields": ["phoneNumber"],
"truncatedArrays": [ {
"field" : "vacation_time",
"newSize" : 36
} ]
},
"fullDocument": {
"_id": ObjectId("58a4eb4a30c75625e00d2820"),
"name": "Alice",
"userName": "alice123",
"email": "alice@10gen.com",
"team": "replication"
}
}

The fullDocument document represents the most current majority-committed version of the updated document. The fullDocument document may vary from the document at the time of the update operation depending on the number of interleaving majority-committed operations that occur between the update operation and the document lookup.

Back

replace