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 is and no document matches the query criteria, inserts a single document. The update creates the new document with either:
- The fields and values of the parameter if the parameter contains only field and value pairs, or
- The fields and values of both the and parameters if the parameter contains update operator expressions. The update creates a base document from the equality clauses in the parameter, and then applies the update expressions from the parameter.
If is and there are documents that match the query criteria, performs an update.
Let us try to know this with an example.Currently there are not documents in db. Run below mongodb upsert operation
- 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.
- What are good bounce rate benchmarks
- JetBrains When PhpStorm 3 will be released
- Who is Googles ad agency
- What is an example of proactive interference
- How are languages learned
- Can anyone deduce like Sherlock
- Do Iranians like Palestinians
- Why Tel Aviv is costly
- Will America have its own feudal era
- Does ammonia smell like rotten eggs
- What is a CEMA mortgage
- How good is the workout app Seven
- What are some websites similar to fetlife
- Are online courses worth the price
- What is the chemical composition of diazepam
- How do I dab without wasting oil
- Why were chalkboards replaced with whiteboards
- How do I hire 1099 drivers
- How does amniocentesis works