0.2.3 • Published 4 years ago

mongoose-find-and-paginate v0.2.3

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

mongoose-find-and-paginate

Build Status

A Mongoose plugin that provides a method, findAndPaginate(), for performing a find and a calculation pagination parameters at the same time.

Install

npm install mongoose-find-and-paginate --save

API

findAndPaginate

Parameters

  • filter Object The filter
  • options Object The Mongoose.Query options. Used for pagination:
    • options.page number The page number (optional).
    • options.perPage number The number of documents on page (optional).
    • options.skip number The number of documents to skip (optional). Used only if the page is not set.
    • options.offset number Alias for skip (optional). Used only if the page is not set.
    • options.limit number Maximum number of documents (optional). Used only if the page is not set.
  • callback Function

Returns Mongoose.Query<QueryResult>

QueryResult

Type: Object

Properties

Usage

Add plugin

import mongoose from 'mongoose';
import findAndPaginatePlugin from 'mongoose-find-and-paginate';

const schema = new mongoose.Schema({ ... });
schema.plugin(findAndPaginatePlugin);
const Model = mongoose.model('Model', schema);

Use skip(offset) & limit

const filter = { ... };
const { docs, totalDocs } = await Model.findAndPaginate(filter, {
  skip: 10,
  limit: 5,
  sort: '_id'
});

Use page & perPage

const filter = { ... };
const { docs, totalPages } = await Model.findAndPaginate(filter, {
  page: 3,
  perPage: 5,
  sort: '_id'
});

Use with Mongoose.Query methods

const filter = { ... };
const query = Model
  .findAndPaginate(filter, { page: 3, perPage: 5 })
  .populate(...)
  .select(...);

const { docs, totalPages } = await query.exec();
0.2.3

4 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago