title: MongoDB C# Driver - Fastest way to perform an "IN" query on _id
body: <p>I'm trying to get values from a collection, based on items whose IDs are in a certain collection of IDs.</p>
  
  <p>My current code to build the filter is:</p>
  
  <pre><code>        IEnumerable&lt;string&gt; IDList;
  
          using (var enumerator = IDList.GetEnumerator())
          {
              if (enumerator.MoveNext() == false) return null; // empty collection
  
              // take the first key
              var key = enumerator.Current;
              filter = Builders&lt;MyClass&gt;.Filter.Eq(p =&gt; p.Key, key);
  
              // take all the other keys
              while (enumerator.MoveNext())
              {
                  var innerKey = enumerator.Current;
                  filter = filter | Builders&lt;MyClass&gt;.Filter.Eq(p =&gt; p.Key, innerKey);
              }
          }
  </code></pre>
  
  <p>and then my code to get the items is:</p>
  
  <pre><code>        List&lt;MyClass&gt; values = new List&lt;MyClass&gt;();
  
          using (var cursor = await MyCollection.FindAsync(filter))
          {
              while (await cursor.MoveNextAsync())
              {
                  values.AddRange(cursor.Current);
              }
          }
  </code></pre>
  
  <p>This code's performance seems pretty subpar, and I'm sure there has to be a faster way since MongoDB should have very good performance... Not to mention I'm querying an indexed field, which should make the query very fast. What can I do to speed this up, both in an async way and a sync way? From some Googling I've seen that there are many ways to query a collection, and I'm not sure which way would be the best for my particular case.</p>
  
  <p>Running this query in RoboMongo takes 0.02 seconds, while running it in C# MongoDb.Driver takes a full second, sometimes even longer and I'm not sure why.</p>
  
  <p>Thanks in advance.</p>