title: C# - Cosmos DB bulk upsert
body: <p>I have an Azure function triggered by a timer in which I want to update documents inside CosmosDB. Now I'm using the function <code>UpdateOneAsync</code> with option <code>IsUpsert = true</code> to make the update (or insert if the document doesn't exist). </p>
  
  <p>However I'm doing the update operation inside a foreach loop, therefore an update operation is performed foreach item. How can I do a bulk update (upsert), performing just one operation after the foreach loop finishes?</p>
  
  <p>Here it is my code right now:</p>
  
  <pre><code>foreach (var group in GetGroups(date, time, hour))
  {
      dic = new MyDictionary&lt;string&gt;();
  
      //... some operations
  
      List&lt;BsonElement&gt; documents = new List&lt;BsonElement&gt;();
      documents.Add(new BsonElement("$inc", new BsonDocument(dic)));
      documents.Add(new BsonElement("$set", new BsonDocument(new Dictionary&lt;string, string&gt;() { { "c", key }, { "d", date } })));
  
      var doc = clicksDoc.UpdateOneAsync(t =&gt; t["_id"] == "c-" + key + "-" + date, new BsonDocument(documents), new UpdateOptions() { IsUpsert = true }).Result;
  }
  </code></pre>
  
  <p>Instead I'd like to perform just one update after the loop. How can I do that?</p>