title: Does MongoDB support Upsert to an Array Field
body: <p>I have a document structured as follows:</p>
  
  <pre><code>{
      "_id" : 1000001,
      "ListMembers" : [
              {
                      "MemberID" : 1000,
                      "Notes" : "Test Notes for 1000"
              },
              {
                      "MemberID" : 1001,
                      "Notes" : "Test Notes for 1002"
              }
      ]
  </code></pre>
  
  <p>}</p>
  
  <p>The application generates a unique Id.
  I need to achieve following:</p>
  
  <ol>
  <li>If the document doesn't exist then Insert a new document with Array
  Field. </li>
  <li>If the document exits in the collection and Member record
  exists in the array then Update the Member Notes</li>
  <li>If the document exits in the collection and Member record doesn't
  exist in the array then Insert Member Record in the array.</li>
  </ol>
  
  <p>Here #1 and #2 work fine but the {upsert:true} option doesn't work for #3 above; if the document exists but the Member Record doesn't exist, it tries to Insert a new document with the same Id which obviously gives me a Duplicate key error. Does MongoDB support UPSERT operation in this scenario OR do I need to check the existence of the document and then do INSERT for #1 and ($pull and $push) to achieve #2 and #3 on the array field.</p>