mongodb upsert operation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Looking forward in update query, we will look into mongodb upsert operation. This is part of update query where upsert option is provided. This article will help you understand what mongodb upsert operation is and give an example.

Mongodb Upsert Operation

Let us see the syntax of update query once again.

We have seen about <query> and <update> parts in previous article. Let us try to understand what upsert means.

This is directory from official documentation.

If upsert is true and no document matches the query criteria, update() inserts a single document. The update creates the new document with either:

  • The fields and values of the <update> parameter if the <update> parameter contains only field and value pairs, or
  • The fields and values of both the <query> and <update> parameters if the <update> parameter contains update operator expressions. The update creates a base document from the equality clauses in the <query> parameter, and then applies the update expressions from the <update>parameter.

If upsert is true and there are documents that match the query criteria, update() performs an update.

Let us try to know this with an example.Currently there are not documents in db. Run below mongodb upsert operation

This returns

  • No documents matched the criteria so nMatched=0
  • one document is inserted so nUpSerted=1
  • No documents is modified so nModified=0

now retrieve all documents using db.employees.find().pretty();

In this operation, only the fields in <update> part of query are inserted in database and not from <query>. This adheres to first bullet point in documentation.

Now let us write a query that does not match any criteria, update some fields using update operations like $set and do mongodb upsert operation for it.

After query execution, result is

  • There is no query match because there is no field with type=”FT” existing in database (because of previous rule) hence nMatched = 0. Same reason for nUpserted = 1 and nModified = 0

check documents in database using db.employees.find().pretty(); You will see two documents

Note that type = “FT” is also added as field now because we have performed an update operation using update operator i.e. $set. This adheres to second bullet point about mongodb upsert operation.

Now let us write another query.

WriteResult for this is

one document matched the update criteria and 1 is modified whereas no document is inserted this time. Let us confirm by listing out all documents.

Note that the document had type=”FT” is now updated  to have salary = 400000.

I hope this article helped understand mongodb upsert operation.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Leave a Reply

Your email address will not be published. Required fields are marked *