title: MongoDB C# Iteration as enumerator
body: <p>I have a large collection of items with MongoDB (> 10m).</p>
  
  <p>The code is in C# / .NET.</p>
  
  <p>On occasion, I need to iterate through all the documents to prune data, do some other maintenance, etc.
  also, in some cases, I need to be able to iterate through all documents but only get the Id of each document.</p>
  
  <p>I want the documents to be presents as an IEnumerable for consumption by code which is used to process lists, etc.</p>
  
  <p>I did the following:</p>
  
  <pre><code>    private static IAsyncCursor&lt;MongoAlbum&gt; GetCursor(Parameters...)
      {
          var F = MakeFilter(Parameters...);
          var Cursor = Mongo.Driver.FindAsync(F).Result;
          return Cursor;
      }
  
      internal static IEnumerable&lt;string&gt; IterateThroughAlbumIds(Parameters...)
      {
          using (var Cursor = GetCursor(Parameters...))
          {
              while (Cursor.MoveNextAsync().Result)
              {
                  var Batch = Cursor.Current;
                  foreach (var Document in Batch) yield return Document._id;
              }
          }
      }
  
      internal static IEnumerable&lt;MongoAlbum&gt; IterateThroughAlbums(Parameters...)
      {
          using (var Cursor = GetCursor(Parameters...))
          {
              while (Cursor.MoveNextAsync().Result)
              {
                  var Batch = Cursor.Current;
                  foreach (var Document in Batch) yield return Document;
              }
          }
      }
  </code></pre>
  
  <p>Now I would like to know two things:</p>
  
  <ul>
  <li>Is there a better way to make an async enumeration look like a .NET IEnumerable?</li>
  <li>During the enumeration, how can I tell the driver to only return the id of a document?</li>
  </ul>