title: C# official MongoDB driver code for updating(replacing) inner array in document in MongoDB
body: <p>I ve got situation like described in this <a href="https://stackoverflow.com/questions/7425993/how-to-update-value-of-specific-embedded-document-inside-an-array-of-a-specifi">question</a></p>
  
  <p>I need some help to write this code using official MongoDb C# driver.</p>
  
  <p>I not only want to update one item from document array but also want to create new one if there is no such item with _arrayId. Also there shouldn't be two array items with the same _arrayId (I have tried to use <code>AddToSet</code> but with no luck).</p>
  
  <p>It's a big headache; I hope there is easier way to achieve this.</p>
  
  <pre><code>var actionUpdate = Update.Set("Votes.$", vote.ToBsonDocument());
  var actionPush = Update.Push("Votes", vote.ToBsonDocument());
  var actionPull = Update.Pull("UserId", vote.UserId);
  
  var querySpecific = Query.And(
      Query.EQ("_id", new ObjectId(_contentId)), 
      Query.EQ("Votes.UserId", vote.UserId));
  var queryPromise = Query.EQ("_id", new ObjectId(_contentId));
  
  MongoCollection collection = null; 
  
  if (_type == ContentType.Promise)
      collection = DataService.PromiseSet.Collection;
  
  var userVotes = collection.Count(querySpecific);
  if (userVotes == 1)
  {
      collection.Update(querySpecific, actionUpdate);
  }
  else if (userVotes == 0)
  {
      collection.Update(queryPromise, actionPush);
  }
  else
  {
      collection.Update(queryPromise, actionPull);
      collection.Update(queryPromise, actionPush);
  }
  </code></pre>
  
  <p>I appreciate any help, thanks.</p>