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 🙏

© 2024 – Pkg Stats / Ryan Hefner

paginator-for-mongoose

v3.2.0

Published

A simple pagination libray for mongoose

Downloads

3

Readme

Paginator For Mongoose

This library provides elegent way to paginate both simple and aggregate mongoose query.

Installation

npm install --save paginator-for-mongoose

Usage

Simple Query


const {paginate} = require('paginator-for-mongoose');

// usage with some mongoose model

let page = 1;

// limit is optional
const limit = 15;

const results = await paginate(Model.find({status: 'active'}), page, limit);

With Aggregate


const {paginateAggregate} = require('paginator-for-mongoose');

// usage with some mongoose model

let page = 1;

// limit is optional
const limit = 15;

const results = await paginateAggregate(Model.aggregate([{ $match: {status: 'active'} }]), page, limit);

Usage with Koa

const mongoose = require('mongoose');
const {enablePlugin, plugin} = require('paginator-for-mongoose');

// call this method to use schema plugin
enablePlugin(mongoose);

// or you can enable like mongoose.plugin(plugin);

// example usage in koajs controller

let page = 1;

// limit is optional, by default 15 items per page
// to change limit set ctx.PER_PAGE = 50, this will set 50 items per page

const results = await Model.aggregate([{ $match: {status: 'active'} }]).paginateAggregate(ctx);

// or for simple query
const results = await Model.find({status: 'active'}).paginate(ctx);

Using plugin on specific models

const mongoose = require('mongoose');
const {plugin} = require('paginator-for-mongoose');


Model.plugin(plugin);

// example usage in koajs controller

let page = 1;

// limit is optional
const limit = 15;

const results = await Model.aggregate([{ $match: {status: 'active'} }]).paginateAggregate(ctx);

// or for simple query
const results = await Model.find({status: 'active'}).paginate(ctx);

Ver 3

Paginate Using ID for large collections

 paginateById(User.find({ role: 'user' }))
        .then((docs) => {
          console.log(docs);
        })
        .catch((e) => console.error(e));

To paginate, pass last document id as second parameter. Third parameter is limit (Default: 50).

// pass last document id as second parameter
 paginateById(User.find({ role: 'user' }), '5e25a641beac1d5978ad2c14')
        .then((docs) => {
          console.timeEnd('Pagi');
          console.log(docs);
        })
        .catch((e) => console.error(e));

Using aggregate query. Second argument is id of last docuemnt and is optional for first page.

 paginateAggregateById(User.aggregate([{ $match: { role: 'user' } }]), '5e25a641beac1d5978ad2c14')
        .then((docs) => {
          console.log(docs);
        })
        .catch((e) => console.error(e));

Note: Pagination using id does not returns documents count because count is slow on large collections.