My User and Post models are set up, with User.favPosts being an array of references to the Post model. When I call paginate like this:
options = { populate: {path: 'favPosts'} };
const result = await User.paginate({}, options)
The resulting user document is now populated with posts:
{
"docs": [
{
"_id": "6299ffa5c2ca4cdeebd1f513",
"name": "user",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4b2e262a22270b262a222765282426">[email protected]</a>",
"favPosts": [
{
"_id": "629b299897f46f31761ad7a7",
"title": "Available Test 5",
"description": "Lorem ipsum dolor sit"
},
{
"_id": "629b1edf108e765744d2560d",
"title": "Available Test 4",
"description": "Lorem ipsum dolor sit"
},
{
"_id": "629b1c0027bf0eb197c057dd",
"title": "Available Test 4",
"description": "Lorem ipsum dolor sit"
}
]
}
],
"totalDocs": 1,
"offset": 0,
"limit": 10,
"totalPages": 1,
"page": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": false,
"prevPage": null,
"nextPage": null
}
I am currently able to paginate users, but I want to be able to paginate the populated documents (posts) as well, including getting the total count, page numbering, etc. The desired result should look like this:
{
"docs": [
{
"_id": "629b299897f46f31761ad7a7",
"title": "Available Test 5",
"description": "Lorem ipsum dolor sit"
},
{
"_id": "629b1edf108e765744d2560d",
"title": "Available Test 4",
"description": "Lorem ipsum dolor sit"
},
{
"_id": "629b1c0027bf0eb197c057dd",
"title": "Available Test 4",
"description": "Lorem ipsum dolor sit"
}
],
"totalDocs": 3,
"offset": 0,
"limit": 10,
"totalPages": 1,
"page": 1,
"pagingCounter": 1,
"hasPrevPage": false,
"hasNextPage": false,
"prevPage": null,
"nextPage": null
}
Is it possible to achieve this using mongoose-paginate-v2?