npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@penname/mongoose-page-aggregation

v1.0.4

Published

Standalone MongoDB Mongoose pagination utility via the power of aggregation pipelines

Readme

Mongoose Page Aggregation

A standalone MongoDB Mongoose pagination utility powered by aggregation pipelines. This library provides efficient cursor-based pagination for MongoDB queries using Mongoose.

npm version License: MIT

Features

  • Cursor-based Pagination: Efficient pagination using MongoDB aggregation pipelines
  • Flexible Sorting: Support for ascending and descending sort orders
  • Custom Queries: Filter documents with custom find queries
  • Population Support: Populate related documents using Mongoose populators
  • Grouping: Group results by specified fields with optional aggregations
  • Type-Safe: Full TypeScript support with comprehensive type definitions
  • Lightweight: Zero dependencies (Mongoose is a peer dependency)
  • ESM & CJS: Supports both ES modules and CommonJS

Installation

npm install @penname/mongoose-page-aggregation mongoose

Or with yarn:

yarn add @penname/mongoose-page-aggregation mongoose

Requirements

  • Node.js: 14.0.0 or higher
  • Mongoose: 8.7.0 or higher

Quick Start

import mongoose from 'mongoose'
import { pageAggregation, DbPagerSortOrder } from '@penname/mongoose-page-aggregation'

// Define your schema
const UserSchema = new mongoose.Schema({
  name: String,
  email: String,
  createdAt: { type: Date, default: Date.now },
  active: { type: Boolean, default: true }
})

const User = mongoose.model('User', UserSchema)

// Basic pagination
async function getUsers() {
  const result = await pageAggregation({
    Model: User,
    findQuery: { active: true },
    pager: {
      field: 'createdAt',
      limit: 10,
      sortOrder: DbPagerSortOrder.Descending
    }
  })

  console.log('Users:', result.dbObjects)
  console.log('Total:', result.total)
  console.log('Can load more:', result.canLoadMore)
}

API Reference

pageAggregation(config)

Main function for paginated queries.

Parameters

config: PageAggregationConfig

  • Model (required): Mongoose model to query
  • findQuery (required): MongoDB query filter object
  • pager (required): Pagination configuration
    • field (required): Field to paginate on (typically a date or ID)
    • limit (optional): Number of documents per page (default: 10)
    • sortOrder (optional): DbPagerSortOrder.Ascending or DbPagerSortOrder.Descending
    • sortAsc (optional): Boolean alternative to sortOrder
    • from (optional): Cursor object for fetching next page
    • groupBy (optional): Group results by specified fields
  • shouldSecordarySortOnId (optional): Enable secondary sorting by _id
  • populators (optional): Array of population configurations
  • postPopulatorFilter (optional): Filter after population
  • skipCache (optional): Skip caching for this query
  • runQuery (optional): Custom query execution function

Returns

PageAggregationResult<D>

  • dbObjects: Array of paginated documents
  • total: Total count of matching documents
  • size: Number of documents in current page
  • limit: Limit used for pagination
  • canLoadMore: Whether more documents are available
  • from: Cursor object for next page
  • sortOrder: Sort order used
  • startedAt: ISO timestamp when query started
  • finishedAt: ISO timestamp when query finished

Examples

Pagination with Cursor

// First page
const firstPage = await pageAggregation({
  Model: User,
  findQuery: { active: true },
  pager: {
    field: 'createdAt',
    limit: 10,
    sortOrder: DbPagerSortOrder.Descending
  }
})

// Next page
if (firstPage.canLoadMore) {
  const nextPage = await pageAggregation({
    Model: User,
    findQuery: { active: true },
    pager: {
      field: 'createdAt',
      limit: 10,
      sortOrder: DbPagerSortOrder.Descending,
      from: firstPage.from
    }
  })
}

Grouping Results

const grouped = await pageAggregation({
  Model: User,
  findQuery: { active: true },
  pager: {
    field: 'createdAt',
    limit: 10,
    groupBy: {
      fields: ['department'],
      countAs: 'count'
    }
  }
})

Population

const withPosts = await pageAggregation({
  Model: User,
  findQuery: { active: true },
  pager: {
    field: 'createdAt',
    limit: 10
  },
  populators: [
    {
      Model: Post,
      localField: '_id',
      targetField: 'userId',
      as: 'posts'
    }
  ]
})

Utility Functions

The library exports several utility functions:

  • isValidId(id): Check if a string is a valid MongoDB ObjectId
  • toObjectId(id): Convert a string to MongoDB ObjectId
  • isValidDate(value): Check if a value is a valid date
  • getModelCollectionName(model): Get collection name from Mongoose model
  • compute(fn): Execute a function and return its result

License

MIT © 2025 PennameHq

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.