When implementing pagination in MongoDB, an important consideration is how to efficiently retrieve both paginated data and the total count of records. A common question developers face is whether to fetch the total count within the same aggregation function or as a separate query. In this article, we will explore two different approaches — using a single aggregation pipeline with $facet , and separating count and data retrieval into distinct queries. We'll discuss their advantages, disadvantages, and when to use each approach for optimal performance. Approach 1: Single Aggregation Pipeline Using $facet MongoDB’s $facet stage allows you to process multiple aggregation pipelines in a single query. This means you can get both the paginated data and the total count simultaneously. Example Aggregation with $facet : const aggregateWithTotalCount = ( filters, sortField, sortOrder, page, pageSize ) => { return [ { $match : filters, }, { $facet : { ...