Mongodb how to insert ONLY if does not exists (no update if exist)? : <p>How can I insert a document if it does not exist while not updating existing document if it exists? </p>

<p>let's say I have a document as follows:</p>

<pre><code>  {
          "company":"test", 
          "name":"nameVal"
}
</code></pre>

<p>I want to check whether the collection contains company <code>test</code>, if it doesn't exist I want to create a new document. If it exists I want to do NOTHING. I tried <code>update</code> with <code>upsert = true</code>. But it updates the existing document if it exists. </p>

<p>This is what I tried: </p>

<pre><code>db.getCollection('companies').update(
    {
  "company": "test"
  },
  {
  "company": "test",
  "name": "nameVal2"
},
{upsert:true}
)
</code></pre>

<p>Appreciate any help to resolve this using one query. </p>
