title: Why is mongodb collection count deprecated?
body: <p>I am using mongodb and noticed that we should use not <code>count()</code> function over a filter query by getting this warning:</p>
  <pre><code>@deprecated Use countDocuments or estimatedDocumentCount
  </code></pre>
  <p><a href="http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#count" rel="nofollow noreferrer">http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#count</a></p>
  <p>The suggested alternative <code>countDocuments()</code> uses aggregation which in certain cases proves to be 20 or more times slower for us (from 0.5 second on 10k filtered out documents vs 20 seconds which makes response time unbearable). It is claimed that the <code>count()</code> function might be inaccurate although I have never witnessed this myself. In what cases can it be inaccurate? Is there an article explaining why it got deprecated in the first place? What happens behind the scenes of count which is worth deprecation over alternative that uses aggregation (<code>countDocuments</code>)?</p>
  <p>For reference, countDocuments implementation looks like this:</p>
  <pre><code>class CountDocumentsOperation extends AggregateOperation {
    constructor(collection, query, options) {
      const pipeline = [{ $match: query }];
      if (typeof options.skip === 'number') {
        pipeline.push({ $skip: options.skip });
      }
      if (typeof options.limit === 'number') {
        pipeline.push({ $limit: options.limit });
      }
      pipeline.push({ $group: { _id: 1, n: { $sum: 1 } } });
      super(collection, pipeline, options);
    }
  </code></pre>