MongoDB provides different types of field update operators to update the values of the fields of the documents and $setOnInsert operator is one of them. This operator is used when a new document is inserted with the help of update operation by setting the value of an upsert field to true, then $setOneInsert
operator assigns the specified value to the fields in the document. If the update operation will not used to insert new document, then $setOnInsert
operator will not do anything.
$setOnInsert operator can also work with embedded/nested documents. You can use this operator in methods like update(), findAndModify() etc., according to your requirements. If the update() or findAndModify() method with upsert: true had found a matching document, then mongoDB will ignore $setOnInsert and apply $set operator, like as shown below in the example 3.
Syntax:
db.collection.update(
<query>,
{ $setOnInsert: { <field1>: <value1>, <field1>: <value2>, ... } },
{ upsert: true }
)
To specify field in embedded/nested documents with the help of dot notation.
In the following examples, we are working with:
Database: GeeksforGeeks
Collection: Example
Document: one documents that contain the details of the employees in the form of field-value pairs.

Inserting new fields in new documents using $setOnInsert
:
In this example, we are creating a new document in the Example collection with the help of update() method by setting the value of an upsert field to true and using $setOneInsert operator assign the values to the department and salary fields in the document.
db.Example.update({name: {first: "Mina" , last: "Singh" },
... personalDetails: {age: 24 , contactInfo: 345678901 }},
... { $setOnInsert: {department: "Development" , salary: 45000 }},
... {upsert: true}
... )
|

Inserting new embedded fields in new document using $setOnInsert
:
In this example, we are creating a new document in the Example collection with the help of update() method by setting the value of an upsert field to true and using $setOneInsert
operator assign the values to embedded fields i.e., in the document.
db.Example.update({ "name.first" : "Vinod" , expreienceYear: 5 },
{$setOnInsert: { "personalDetails.age" : 27 ,
"personalDetails.contactInfo" : 345678901 }},
{upsert: true})
|

Effect of $setOnInsert
operator on Matched documents:
In this example, we are checking if $setOnInsert operator work with matched document or not. This $setOnInsert operator does not work with already present documents. Or in other words, MongoDB will ignore this operator when the update() method found the matched document.
db.Example.update({ "name.first" : "Mina" },
{$setOnInsert: { "personalDetails.age" : 27 ,
"personalDetails.contactInfo" : 345678001 }},
{upsert: true})
|
Before:

After:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
10 May, 2020
Like Article
Save Article