mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

mongoose virtual populate returnCount option

Open thatisuday opened this issue 10 years ago • 7 comments

I am using mongoose v4.5.3

I am guessing, virtual populate method use find to populate data or findOne method when justOne is set to true. Can you please add returnCount option when it uses count query and populate document count?

Or please guide me to make the changes by myself so that I could give you a PR.

thatisuday avatar Aug 29 '16 19:08 thatisuday

Actually it doesn't use findOne() when you use justOne(), because then we'd have to execute a separate populate query for every document that we're trying to populate. Using count() would have the same problem, right now the way populate works is that it executes one big query for every document you're trying to populate and then looks through the results to determine which document the query result belongs in. Interesting suggestion though, will consider for a future release.

vkarpov15 avatar Sep 02 '16 18:09 vkarpov15

I indeed like this feature. I'm building a restful api system that responses all data by calling a single request. But the limitation is that mongoose cannot return the count of the population field. It leads to the difficulty of paging because we don't know the total records of populated objects. Without this, I must call some queries to just get the total records.

It's very great if mongoose has an option that can choose returning only data, only count or both.

For example:

Only data:

{
  "status": "ok",
  "data": {
    "_id": "582992e76101e20c2100711a",
    "songs": [
      {
        "_id": "582992fa6101e20c2101f205",
        "id": 791
      },
      {
        "_id": "582992fa6101e20c2101f4e2",
        "id": 1541
      },
      {
        "_id": "582992fa6101e20c2101f63b",
        "id": 1899
      },
      {
        "_id": "582992fa6101e20c2101f65c",
        "id": 1932
      },
      {
        "_id": "582992fa6101e20c2101f674",
        "id": 1956
      }
    ]
  }
}

Only count:

{
  "status": "ok",
  "data": {
    "_id": "582992e76101e20c2100711a",
    "songs": 100
  }
}

Or both:

{
  "status": "ok",
  "data": {
    "_id": "582992e76101e20c2100711a",
    "songs": {
        "data": [
      {
        "_id": "582992fa6101e20c2101f205",
        "id": 791
      },
      {
        "_id": "582992fa6101e20c2101f4e2",
        "id": 1541
      },
      {
        "_id": "582992fa6101e20c2101f63b",
        "id": 1899
      },
      {
        "_id": "582992fa6101e20c2101f65c",
        "id": 1932
      },
      {
        "_id": "582992fa6101e20c2101f674",
        "id": 1956
      }
    ],
        "count": 100 
    }
  }
}

nguyentrongbang avatar Nov 23 '16 07:11 nguyentrongbang

I'm building a restful API on top of mongoose too and this feature would be beyond awesome. Are there any updates on this?

danii-nebot avatar Feb 23 '17 09:02 danii-nebot

Nope no updates. PRs would be welcome though.

vkarpov15 avatar Mar 02 '17 03:03 vkarpov15

I am also building restful API. This would be a huge feature. Any updates?

kristofferabell avatar Mar 21 '17 13:03 kristofferabell

Would be interested in this too

kgriffiths avatar Nov 14 '18 21:11 kgriffiths

Will add this for 5.4 :+1:

vkarpov15 avatar Nov 17 '18 18:11 vkarpov15