Use case for upsert with examples

Could anyone explain the use cases for upsert with examples?

#1) upsert= false, – in this example here I want to know why upsert = true, does not work

#2) upsert=true, – in this example here I want to know why upsert = false, does not work

I think just like anytime you have insert new doc of update existing one if already exists kind of situation, you can use upsert to ease the logic of the app code

Thank you for your response. But, what is the purpose of those 2 options i.e., upsert=false and upsert=true

Hi @suresh_pamujula,

When upsert is set to true , the update command creates a new document when no document matches the query criteria specified in your update command.

When upsert is set to false, the update command does not insert a new document when no match is found.

You can read more about it here.

Let me know if you still have any question.

~ Shubham

shubham,
#1 - When upsert is set to true, what happens when there is a document that matches the query criteria? I assume, it updates the existing document. So, then what is the purpose of setting to “True”
#2 - When upsert is set to false, what is the use of this setting, if it doesn’t not insert new document if there is no match. I understand upsert is set to false by default.

I’d represent the possibilities like this:

query upsert:true upsert:false
match update update
no match insert nothing

Just in case, this is the table html code:

<table>
  <tr>
    <th>query</th>
    <th>upsert:true</th>
    <th>upsert:false</th> 
  </tr>
  <tr>
    <td>match</td>
    <td>update</td>
    <td>update</td>
  </tr>
  <tr>
    <td>no match</td>
    <td>insert</td>
    <td>nothing</td>
  </tr>
</table>
1 Like