question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mongoose virtual populate returnCount option

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

4reactions
vkarpov15commented, Nov 17, 2018

Will add this for 5.4 👍

2reactions
nguyentrongbangcommented, Nov 23, 2016

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 
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Populate Virtual Option Count returns whole ...
The mongoose populate virtual count is supposed to return the total number of documents that satisfies the ref, localField and foreignField. But ...
Read more >
Mongoose v6.8.2: Query Population
Arrays of refs work the same way. Just call the populate method on the query and an array of documents will be returned...
Read more >
Populating virtual count field returns the sum of all referencing ...
It returns total number of references in the reference array (replies) instead of number of reports for those referenced replies. Version of ...
Read more >
Using MongoDB with Mongoose — Populate Virtuals and Count
In this article, we'll look at how to use Mongoose to manipulate our MongoDB database. Populate Virtuals: The Count Option. We can add...
Read more >
The deep virtual population in mongoose is actually very simple!
mongoose's virtual population is very useful tool. ... it has second parameter which returns the populate documents, original documents also get changed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found