title: Why I lose performance if I use LINQ on MongoDB?
body: <p>This is the situation. I have a Domain object <code>Product</code> like this...</p>
  
  <pre><code>[DataContract]
  public class Product : IStorableEntity
  {
      [DataMember]
      public String Id { get; set; }
      [DataMember]
      public String RemoteId { get; set; }
      [DataMember]
      public String LanguageId { get; set; }
      [DataMember]
      public DateTime? CreationDate { get; set; }
      [DataMember]
      public DateTime? LastUpdate { get; set; }
      ETC..ETC...
  }
  </code></pre>
  
  <p>into my repository layer I have the following method.</p>
  
  <pre><code>public IEnumerable&lt;TElement&gt; Read()
  {
      var mongoCollection = _mongoDatabase.GetCollection&lt;TElement&gt;(_partitionName);
      return mongoCollection.AsQueryable&lt;TElement&gt;();
  
  }
  </code></pre>
  
  <p>With this method I want to expose via LINQ my repository layer without exporting information about technology.</p>
  
  <p>In this way I can do this:</p>
  
  <pre><code>var _repository = new MyRepositoryFactory&lt;Product&gt;(); 
  var result = _repository.Read().Where(p=&gt;p.RemoteId == "1")
  </code></pre>
  
  <p>this query it takes 1 or 2 milliseconds.</p>
  
  <p>instead...</p>
  
  <pre><code>var _repository = new MyRepositoryFactory&lt;Product&gt;();
  var result = _repository.Read().Where(p=&gt;p.RemoteId == "29000")
  </code></pre>
  
  <p><strong>it takes 2800 milliseconds!!!</strong> </p>
  
  <p>I have correctly created a unique index with the command </p>
  
  <p><code>db.products.ensureIndex({"RemoteId":1, unique:true})</code></p>
  
  <p><em>NB: Yes, I have also rebuilt the indexes with <code>.reIndex()</code> command</em></p>
  
  <p>Here the strange thing...
  Avoiding LINQ and modifying the repository method in...</p>
  
  <pre><code>public IEnumerable&lt;TElement&gt; Read(string remoteId)
  {
      var mongoCollection = _mongoDatabase.GetCollection&lt;TElement&gt;(_partitionName);
        var query = Query&lt;TElement&gt;.EQ(p =&gt; p.RemoteId, remoteId);
      return mongoCollection.Find(query);
  }
  </code></pre>
  
  <p>if then I invoke the method whit the same id before..</p>
  
  <pre><code>var _repository = new MyMongoRepository&lt;Product&gt;();
  var result = _repository.Read("29000")
  </code></pre>
  
  <p>it takes 1 or 2 milliseconds. WHY??</p>
  
  <p>Why with the first approach do I have a performance degradation as the id increases instead with the second is not it?  </p>
  
  <p>Ps. Erm... really sorry for my english</p>