title: Is a MongoDB bulk upsert possible? C# Driver
body: <p>I'd like to do a bulk upsert in Mongo. Basically I'm getting a list of objects from a vendor, but I don't know which ones I've gotten before (and need to be updated) vs which ones are new. One by one I could do an upsert, but UpdateMany doesn't work with upsert options.</p>
  
  <p>So I've resorted to selecting the documents, updating in C#, and doing a bulk insert.</p>
  
  <pre><code>    public async Task BulkUpsertData(List&lt;MyObject&gt; newUpsertDatas)
      {
          var usernames = newUpsertDatas.Select(p =&gt; p.Username);
          var filter = Builders&lt;MyObject&gt;.Filter.In(p =&gt; p.Username, usernames);
  
          //Find all records that are in the list of newUpsertDatas (these need to be updated)
          var collection = Db.GetCollection&lt;MyObject&gt;("MyCollection");
          var existingDatas = await collection.Find(filter).ToListAsync();
  
          //loop through all of the new data, 
          foreach (var newUpsertData in newUpsertDatas)
          {
              //and find the matching existing data
              var existingData = existingDatas.FirstOrDefault(p =&gt; p.Id == newUpsertData.Id);
              //If there is existing data, preserve the date created (there are other fields I preserve)
              if (existingData == null)
              {
                  newUpsertData.DateCreated = DateTime.Now;
              }
              else
              {
                  newUpsertData.Id = existingData.Id;
                  newUpsertData.DateCreated = existingData.DateCreated;
              }
          }
  
          await collection.DeleteManyAsync(filter);
          await collection.InsertManyAsync(newUpsertDatas);
      }
  </code></pre>
  
  <p>Is there a more efficient way to do this?</p>
  
  <p>EDIT:</p>
  
  <p>I did some speed tests.</p>
  
  <p>In preparation I inserted 100,000 records of a pretty simple object. Then I upserted 200,000 records into the collection.</p>
  
  <p>Method 1 is as outlined in the question. SelectMany, update in code, DeleteMany, InsertMany. This took approximately 5 seconds.</p>
  
  <p>Method 2 was making a list of UpdateOneModel with Upsert = true and then doing one BulkWriteAsync. This was super slow. I could see the count in the mongo collection increasing so I know it was working. But after about 5 minutes it had only climbed to 107,000 so I canceled it.</p>
  
  <p>I'm still interested if anyone else has a potential solution</p>