title: Explanation of why duplicate entry is made on an upsert
body: <p>Assume I have this collection:</p>
  
  <pre><code>db.item.find().pretty()
  {
      "_id" : ObjectId("5c5635194b6929067972b85c"),
      "name" : "car",
      "attributes" : [
          {
              "color" : "blue",
              "qty" : 10
          }
      ]
  }
  </code></pre>
  
  <p>I want to add a new entry into the embedded "attributes" document if it does not exist, otherwise only do the update. I constructed the following query to do this as per the MongoDb documentation:</p>
  
  <pre><code>db.item.update({"name":"car"}, {"$push" : {"attributes" : {"color": "red", "qty": 20}}}, upsert=true)
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  </code></pre>
  
  <p>This works and I get this:</p>
  
  <pre><code>db.item.find({"name":"car"}).pretty()
  {
      "_id" : ObjectId("5c5635194b6929067972b85c"),
      "name" : "car",
      "attributes" : [
          {
              "color" : "blue",
              "qty" : 10
          },
          {
              "color" : "red",
              "qty" : 20
          }
      ]
  }
  </code></pre>
  
  <p>However, if I run the query again, I get a duplicate entry added:</p>
  
  <pre><code>db.item.update({"name":"car"}, {"$push" : {"attributes" : {"color": "red", "qty": 20}}}, upsert=true)
  WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  &gt; db.item.find({"name":"car"}).pretty()
  {
      "_id" : ObjectId("5c5635194b6929067972b85c"),
      "name" : "car",
      "attributes" : [
          {
              "color" : "blue",
              "qty" : 10
          },
          {
              "color" : "red",
              "qty" : 20
          },
          {
              "color" : "red",
              "qty" : 20
          }
      ]
  }
  </code></pre>
  
  <p>Why does that happen? The upsert should prevent this or am I misunderstanding the function of upsert?</p>