A library for MongoDB aggregation with built-in pagination, using the $facet operator to efficiently combine filtering, counting, and data retrieval in a single query. Version 1.4.0 supports raw MongoDB, Mongoose, and TypeScript. It organizes pipelines into pre-paging (filters), facet (count + data split), and post-paging (lookups/expensive ops) stages to minimize pipeline size early. Differentiates from mongoose-paginate by focusing on aggregation pipelines rather than simple find queries.
npm install mongodb-paginateNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows raw MongoDB pagination with pre-paging filter, post-paging lookup, and paging options.
Ensure all filtering stages are in prePagingStage and all post-paging stages (lookups, etc.) are in postPagingStage.
Use: paginate(MyModel, prePagingStage, postPagingStage, pagingOptions)
Consider passing collection instance directly instead of relying on dbConnection global.
Always check result.totalDocs > 0 before assuming result.pages >= 1.
Set dbConnection.url and dbName before any paginate call, ideally at app startup.
Ensure MongoDB version >= 3.4 and do not embed $facet in prePagingStage or postPagingStage.
Use dynamic import: const paginate = (await import('mongodb-paginate')).default;