aravindnc / mongoose-paginate-v2

A custom pagination library for Mongoose with customizable labels.

Home Page:https://www.npmjs.com/package/mongoose-paginate-v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to paginate through populated documents using the mongoose-paginate-v2?

JKurero opened this issue · comments

Is your feature request related to a problem? Please describe.
I've got User and Post models. User.favPosts is an array of refs to the Post model. I'm calling paginate like this:

options = { populate: {path: 'favPosts'} };
const result = await User.paginate({}, options)

And the result is a user document with populated posts:

{
  "docs": [
    {
      "_id": "6299ffa5c2ca4cdeebd1f513",
      "name": "user",
      "email": "email@mail.com",
      "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
}

So I'm getting the pagination of users. But I want to get pagination of populated documents (posts), so I could get the amount of these, page counting, etc.

Describe the solution you'd like
It would be great to get something like this as a result:

{
  "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
}

Can somebody tell me, is it possible at all?

@aravindnc, thanks, it helped!