Mongoose plugin for keyset (cursor) pagination.
npm install @mongoosejs/cursor-paginationimport 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.
npm test
npm run test:types