Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

insert

On this page

  • Summary
  • Description
  • Example
insert

An insert event occurs when an operation adds documents to 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

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.

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.

New in version 4.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 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.

lsid
document

The identifier for the session associated with the transaction.

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

New in version 4.0.

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

New in version 4.0.

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.

The following example illustrates an insert event:

{
"_id": { <Resume Token> },
"operationType": "insert",
"clusterTime": <Timestamp>,
"wallTime": <ISODate>,
"ns": {
"db": "engineering",
"coll": "users"
},
"documentKey": {
"userName": "alice123",
"_id": ObjectId("599af247bb69cd89961c986d")
},
"fullDocument": {
"_id": ObjectId("599af247bb69cd89961c986d"),
"userName": "alice123",
"name": "Alice"
}
}

The documentKey field includes both the _id and the userName field. This indicates that the engineering.users collection is sharded, with a shard key on userName and _id.

The fullDocument document represents the version of the document at the time of the insert.

← dropIndexes