Skip to content

Repository files navigation

@mongoosejs/cursor-pagination

Mongoose plugin for keyset (cursor) pagination.

npm install @mongoosejs/cursor-pagination
import mongoose from 'mongoose';
import cursorPagination from '@mongoosejs/cursor-pagination';

const postSchema = new mongoose.Schema({
  authorId: mongoose.Schema.Types.ObjectId,
  createdAt: Date
});
postSchema.plugin(cursorPagination);

const posts = await Post.find({ authorId })
  .after(
    { createdAt: '2026-01-14T09:00:00Z', _id: lastId },
    { createdAt: -1, _id: -1 }
  )
  .limit(20)
  .lean();

after() accepts a boundary and sort POJO, and expands the boundary into a lexicographic $or predicate. after() also sets the query's sort(). Existing filters containing $or are preserved by wrapping them in $and.

The sort argument is also used for TypeScript inference: the boundary must contain exactly the sort keys.

Development

npm test
npm run test:types

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages