title: How can I upsert a record and array element at the same time?
body: <p>That is meant to be read as a dual upsert operation, upsert the document then the array element.</p>
  
  <p>So MongoDB is a denormalized store for me (we're event sourced) and one of the things I'm trying to deal with is the concurrent nature of that. The problem is this:</p>
  
  <ul>
  <li>Events can come in out of order, so each update to the database need to be an upsert.</li>
  <li>I need to be able to not only upsert the parent document but an element in an array property of that document.</li>
  </ul>
  
  <p>For example:</p>
  
  <ul>
  <li>If the document doesn't exist, create it. All events in this stream have the document's ID but only part of the information depending on the event.</li>
  <li>If the document does exist, then update it. This is the easy part. The update command is just written as UpdateOneAsync and as an upsert.</li>
  <li>If the event is actually to update a list, then that list element needs to be upserted. So if the document doesn't exist, it needs to be created and the list item will be upserted (resulting in an insert); if the document does exist, then we need to find the element and update it as an upsert, so if the element exists then it is updated otherwise it is inserted.</li>
  </ul>
  
  <p>If at all possible, having it execute as a single atomic operation would be ideal, but if it can only be done in multiple steps, then so be it. I'm getting a number of mixed examples on the net due to the large change in the 2.x driver. Not sure what I'm looking for beyond the UpdateOneAsync. Currently using 2.4.x. Explained examples would be appreciated. TIA</p>
  
  <p>Note:
  Reiterating that this is a question regarding the MongoDB C# driver 2.4.x</p>