Count operation taking a lot of time


We are trying to run a query with match filter. The query for data performs the match, sort, limit, skip and project operations. We also need to get the count of documents filtered by the match condition.

We are doing it through two separate queries. One to get the data and another to get the count. The data query is working pretty fast and returning the results within a second. But the count query is taking around 6-7 seconds. We profiled the query and realised it is performing a group operation:

        "$group": {
          "_id": 1,
          "n": {
            "$sum": 1

We wanted to know is there anyway to get the count of matched documents in a faster manner.