MongoDb Upsert

In this page we will discuss about about the Upsert. Upsert is the variant of update. Upsert says if can not find the document to update, i want to insert one. See the diagram below.
MongoDB Upsert
Pymongo Upsert working
Update Query looks for match, if there no match to update then it goes to upsert to insert, if upsert specified as True.
Pymongo upsert code
Pymongo upsert code
Click to view code
In above code we have set upsert equal to true. Let check the results.
Pymomgo Upsert
Pymongo Upsert operation
The code first check the ID = 201 to update, but when it does not find the desired ID, it goes to upsert, If upsert is true, then it insert the document or records in the collection.