$setOnInsert
The $setOnInsert operator in Amazon DocumentDB is used to set the value of a field if a document is being inserted, but it has no effect if the document is being updated.
Parameters
-
field: The field to be set. -
value: The value to assign to the field.
Example (MongoDB Shell)
The following example demonstrates the usage of the $setOnInsert operator in Amazon DocumentDB. It creates a new document if the document does not already exist, but it has no effect if the document is being updated.
Create sample documents
db.users.insertOne({ _id: 1, name: "John Doe", age: 30 })
Query example 1 - Update existing document
db.users.update( { _id: 1 }, { $set: { age: 31 }, $setOnInsert: { createdAt: new Date() } }, { upsert: true } )
Output 1
{ _id: 1, name: "John Doe", age: 31 }
The output shows that the document was updated, but the createdAt field was NOT added since the document already existed. The $setOnInsert operator only applies when inserting new documents.
Query example 2 - Insert new document (upsert)
db.users.update( { _id: 2 }, { $set: { name: "Jane Smith", age: 25 }, $setOnInsert: { createdAt: new Date() } }, { upsert: true } )
Output 2
{ _id: 2, name: "Jane Smith", age: 25, createdAt: ISODate("2025-10-31T09:57:52.459Z") } }
Code examples
To view a code example for using the $setOnInsert command, choose the tab for the language that you want to use: