on
Italy
- Get link
- X
- Other Apps
$ npm install mongoose-pages
skip method and specify the number of items to be shown per page, and the page number.var mongoose = require('mongoose'); var Schema = mongoose.Schema; var mongoosePages = require('mongoose-pages'); var UserSchema = new Schema({ username: String, points: Number, email: String }) mongoosePages.skip(UserSchema); // makes the findPaginated() method available var docsPerPage = 10; var pageNumber = 1; var User = mongoose.model('User', UserSchema); User.findPaginated({}, function (err, result) { if (err) throw err; console.log(result); }, docsPerPage, pageNumber); // pagination options go hereThe response object of pagination via
skip will have the following structure.{ documents: Array; list of documents totalPages: Number; total number of pages, as per the `docsPerPage` value prevPage: Number; the previous page number nextPage: Number; the next page number }
anchor method. You pass it the number of items to be shown per page, and the anchor id. If you are requesting for the first page, you don't have to specify the anchor id. You will get the anchorId in the response of the first page, if there are more pages; which can then be used to request for the next page.var mongoose = require('mongoose'); var Schema = mongoose.Schema; var mongoosePages = require('mongoose-pages'); var UserSchema = new Schema({ username: String, points: Number, email: String }) mongoosePages.anchor(UserSchema); // makes the findPaginated() method available var docsPerPage = 10; var anchorId = '53c797a2043db36f2b673cd1'; var User = mongoose.model('User', UserSchema); User.findPaginated({}, function (err, result) { if (err) throw err; console.log(result); }, docsPerPage, anchorId); // pagination options go hereThe response object of pagination via
anchor will have the following structure.{ documents: Array; list of documents totalPages: Number; total page count prevAnchorId: String; ObjectId which was used as the anchor id in the last request nextAnchorId: String; ObjectId which should be used as the anchor id in the next request }For more details about Mongoose Pages, reporting bugs and issues, requesting features, please visit the official project page on GitHub.
Comments
Post a Comment