title: Spring boot mongoTemplate count query is slow
body: <p>I'm writing an API to fetch articles from my mongoDB that correspond the passed criteria. So for example, if a user defines a start and end date it will search only within that time range. If he defines a source it will search within that time period only for those specific sources. It all works well enough with the exception of when user doesn't provide any criteria. In that case, I'm supposed to just return 100 articles as defined by default in <code>Pageable</code> param. Excluding the criteria aggregation to a <code>Query</code> object, his is all done like so:</p>
  <pre><code>List&lt;Article&gt; articles = mongoTemplate.find(query, Article.class);
  
  return PageableExecutionUtils.getPage(articles,
          pageable, 
          () -&gt; mongoTemplate.count(Query.of(query).limit(-1).skip(-1), Article.class));
  </code></pre>
  <p>Since query is empty it just fetches all the documents and does the count on all articles. I've tried hardcoding the count part to a random number like 1000. In that case the whole operation takes about 200ms. But if I leave it as above with <code>mongoTemplate.count(...)</code> it takes a whopping 1 minute and 30 seconds. On the other hand, if I do a count query through Robo3t with <code>db.getCollection('articles').find({}).count()</code> it takes 37ms. Most fields are indexed and there are a bit under 600,000 entries, so I've no idea why a count would take this long. Any ideas why such a time discrepancy between robo3t and mongoTemplate and how to fix it?</p>
  <p>I've also tried doing the same query with <code>MongoRepository</code>, same results.</p>