1.0.3 • Published 5 years ago

cursor_paginate v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

cursor_paginate

This module has been developed for mongoose cursor pagination using Mongo range queries or relevancy-based search results.

Installation

npm install cursor_paginate --save

Usage

Define your mongoose schema and plug the cursor_paginate module.

const paginate = require('cursor_paginate');

const testSchema = new mongoose.Schema({
  name: String,
  age: Number,
  mobile : Number,
  userId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',      // reference of user schema
  },
  categoryId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Category',  // reference of category schema
  }
});

testSchema.plugin(paginate);

const Test = mongoose.model('Test', testSchema);

Model.paginate(params)

Parameters

  • params

    • previous {String} - The value to start querying previous page.

    • next {String} - The value to start querying next page.

    • limit {Number} - The page size.

    • fields {Object} - Fields to query in the Mongo object format, e.g. {_id: 1, name :1} . The default is to query all fields.

    • populate - Name of the field or key you want to populate. e.g. { path: 'userId', select: {name:1 }} 'userId' is name of field, 'select' key gives selected fields from reference schema.

Examples

test.paginate(
  previous,
  next,
  limit,
  fields,
  populate
)
.then((result) => {
 ...
})