mongoose virtual populate returnCount option
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.
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.
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
}
}
}
I'm building a restful API on top of mongoose too and this feature would be beyond awesome. Are there any updates on this?
Nope no updates. PRs would be welcome though.
I am also building restful API. This would be a huge feature. Any updates?
Would be interested in this too
Will add this for 5.4 :+1: