How to efficiently create a list of WriteModel in Mongo?
<p>Rather than make a request to the database for every single document retrieved from the query. We can create a List of WriteModel operations instead.</p>

<pre><code>var operationList = new List&lt;WriteModel&lt;JobInfoRecord&gt;&gt;();

using (var cursor = await jobInfoDocuments.Find(filter).Project(projectionDefinition).ToCursorAsync())
{
      while (await cursor.MoveNextAsync())
      {
            var batch = cursor.Current;
            foreach (var document in batch)
            {
                 var newInfo = Regex.Replace(document.SerializedInfo, regex, EmptyTag);

                 // Applying several operations within the one request.
                 operationList.Add(new UpdateOneModel&lt;JobInfoRecord&gt;(Builders&lt;JobInfoRecord&gt;.Filter.Eq("_id", document.Id),
                                                                     Builders&lt;JobInfoRecord&gt;.Update.Set("SerializedBackgroundJobInfo", newInfo)));
             }
</code></pre>

<p>Is it good algorithm to execute once in every 1000 and clear list?</p>

<pre><code>if (operationList.Count &lt;= 1000)
{
    await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
    operationList = new List&lt;WriteModel&lt;JobInfoRecord&gt;&gt;();
}
</code></pre>

<p>And then after <code>while (await cursor.MoveNextAsync())</code> check if we didn't forgot anything</p>

<pre><code>if (operationList.Count &gt; 0)
{
    await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
}
</code></pre>

<p>Or I just can do the following after <code>foreach</code> loop without any <code>if</code> statement: </p>

<pre><code>await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
operationList = new List&lt;WriteModel&lt;JobInfoRecord&gt;&gt;(); 
</code></pre>

<p>What should I do after the <code>foreach</code> loop?</p>