const paginateScheme = new Schema({
posts: [ { img: String, description: String, liked: String, postComId: String, comments: [ { author: String, text: String } ] } ],
}, {collection: "usersData"});
paginateScheme.plugin(mongoosePaginate);
const myModel = mongoose.model("sample", paginateScheme);
app.put("/api/pagination", function(req, res){
const options = {
page: 1,
limit: 3,
collation: {
locale: 'en',
},
};
const query = User.find({mail: 'user2@example.com'}, {posts: 1 });
myModel.paginate( query , options, function (err, result) {
if(err) return console.log(err);
res.send(result);
});
});
where post array of objects which I want to paginate. I checked this plugin works correctly. When I use Model.find({}) it paginates through outer objects without any problems. I tried to use skip + limit but it returned nothing.
this query returns:
{
docs: [ { _id: 601a8f013d86dc237468467c, posts: [Array] } ],
totalDocs: 1,
limit: 3,
totalPages: 1,
page: 1,
pagingCounter: 1,
hasPrevPage: false,
hasNextPage: false,
prevPage: null,
nextPage: null
}
you can using $slice
for paginate a array like this(use async/await)
let result = await myModel.find({mail: 'user2@example.com'}, {posts: {$slice: [0, 3]}})