title: MongoDB BulkWrite ExceededTimeLimit error in .Net
body: <p>I'm trying to push about 150k updates into Mongo database (v 4.2.9 running on Windows, stage replica with two nodes) using BulkWrite on c# driver (v2.11.6) and looks like it is impossible. The project is .Net Framework 4.7.2.</p>
  <p>Mongo c# driver documentation is terrible, but somehow on forums and with a lot of googling, I was finnaly able to <a href="https://dev.to/mpetrinidev/a-guide-to-bulk-write-operations-in-mongodb-with-c-51fk" rel="nofollow noreferrer">find a way</a> how to run about 150k updates using a batch, something like this (a little simplified for SO):</p>
  <pre><code>client = new MongoClient(connString);
  database = client.GetDatabase(db);
  
  // Build all the updates
  List&lt;UpdateOneModel&lt;GroupEntry&gt;&gt; updates = new List&lt;UpdateOneModel&lt;GroupEntry&gt;&gt;();
  foreach (GroupEntry groupEntry in stats)
  {
    FilterDefinition&lt;GroupEntry&gt; filter = Builders&lt;GroupEntry&gt;.Filter.Eq(e =&gt; e.Key, groupEntry.Key);
  
    UpdateDefinitionBuilder&lt;GroupEntry&gt; update = Builders&lt;GroupEntry&gt;.Update;
    var groupEntrySubUpdates = new List&lt;UpdateDefinition&lt;GroupEntry&gt;&gt;();
  
    if (groupEntry.Value.Clicks != 0)
      groupEntrySubUpdates.Add(update.Inc(u =&gt; u.Value.Clicks, groupEntry.Value.Clicks));
  
    if (groupEntry.Value.Position != 0)
      groupEntrySubUpdates.Add(update.Set(u =&gt; u.Value.Position, groupEntry.Value.Position));
  
    UpdateOneModel&lt;GroupEntry&gt; groupEntryUpdate = new UpdateOneModel&lt;GroupEntry&gt;(filter, update.Combine(updates));
    groupEntryUpdate.IsUpsert = true;
  
    updates.Add(groupEntryUpdate);
  }
  
  // Now BulkWrite them in transaction to make sure data are consistent
  IClientSessionHandle session = client.StartSession();
  session.StartTransaction();
  
  IMongoCollection&lt;GroupEntry&gt; collection = database.GetCollection&lt;GroupEntry&gt;(collectionName);
  
  // Following line FAILS after some time
  BulkWriteResult&lt;GroupEntry&gt; bulkWriteResult = collection.BulkWrite(session, updates);
  
  if (!bulkWriteResult.IsAcknowledged)
    throw new Exception(&quot;Mongo BulkWrite is not acknowledged!&quot;);
  
  session.CommitTransaction();
  </code></pre>
  <p>The problem is that I keep getting the following exception:</p>
  <pre><code>{
     &quot;operationTime&quot;:Timestamp(1612737199,
     1),
     &quot;ok&quot;:0.0,
     &quot;errmsg&quot;:&quot;Exec error resulting in state FAILURE :: caused by :: operation was interrupted&quot;,
     &quot;code&quot;:262,
     &quot;codeName&quot;:&quot;ExceededTimeLimit&quot;,
     &quot;$clusterTime&quot;:{
        &quot;clusterTime&quot;:Timestamp(1612737199,
        1),
        &quot;signature&quot;:{
           &quot;hash&quot;:new BinData(0,
           &quot;ljcwS5Gf2JBpEu/OgPFbvRqclLw=&quot;&quot;)&quot;,
           &quot;keyId&quot;:&quot;NumberLong(&quot;&quot;6890288652832735234&quot;&quot;)&quot;
        }
     }
  }
  </code></pre>
  <p>Does anyone have any clue? Mongo c# driver docs are completely useless. It looks like I should somehow set property <strong>$maxTimeMS</strong>, but it is not possible on BulkInsert. I have tried:</p>
  <ul>
  <li>Restarts and rebuilds</li>
  <li>Different versions of MongoDriver</li>
  <li>Set much bigger timeouts for all &quot;timeout&quot; properties on MongoClient and session</li>
  <li>Create smaller batches for BulkWrite (up to 1000 items per batch). Fails after 50-100 updates.</li>
  <li>Spent hours and hours in useless Mongo docs and Mongo JIRA</li>
  </ul>
  <p>So far no luck. The funny thing is, that the same approach works on c# driver 2.10.3 on .Net CORE 3.1 (yes, i tried) even with bigger batches (about 300k updates).</p>
  <p>What am I missing?</p>
  <p><strong>EDIT:</strong></p>
  <p>I tried set maxCommitTime to 25 minutes based on dododo's comments like this:</p>
  <pre><code>IClientSessionHandle session = client.StartSession(new ClientSessionOptions()
  {
    DefaultTransactionOptions = new TransactionOptions(new Optional&lt;ReadConcern&gt;(ReadConcern.Default), 
      new Optional&lt;ReadPreference&gt;(ReadPreference.Primary), 
      new Optional&lt;WriteConcern&gt;(WriteConcern.Acknowledged), 
      new Optional&lt;TimeSpan?&gt;(TimeSpan.FromMinutes(25)))
  });
  </code></pre>
  <p>It now throws exception while doing commmit: <strong>NoSuchTransaction - Transaction 1 has been aborted.</strong>. We checked MongoDB log file and found new error in there:</p>
  <blockquote>
  <p>Aborting transaction with txnNumber 1 on session
  09ea7755-7148-43e8-83d8-8bf58c211bda because it has been running for
  longer than 'transactionLifetimeLimitSeconds</p>
  </blockquote>
  <p>Based on <a href="https://docs.mongodb.com/manual/reference/parameters/" rel="nofollow noreferrer">docs</a>, this is 60 seconds by default. So we set it to 5 minutes and now it works.</p>
  <p>So, thank you dododo for pointing me the right direction.</p>
  <p>Anyway, it would be really great if Mongo team described errors better and write documentation above basic CRUD operations.</p>