MongoDB – SetOnInsert Operator ($setOnInsert)

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Example.update({name: {first: "Mina", last: "Singh"},
... personalDetails: {age: 24, contactInfo: 345678901}},
... { $setOnInsert: {department: "Development", salary: 45000}},
... {upsert: true}
... )

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Example.update({"name.first": "Vinod", expreienceYear: 5}, 
                  {$setOnInsert: {"personalDetails.age": 27
                                  "personalDetails.contactInfo": 345678901}},
                  {upsert: true})

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.Example.update({"name.first": "Mina"}, 
                  {$setOnInsert: {"personalDetails.age": 27
                                  "personalDetails.contactInfo": 345678001}}, 
                  {upsert: true})

chevron_right


Before:

After:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.