Sails: Create query syntax for populate counts

Created on 28 Jan 2015  ·  21Comments  ·  Source: balderdashy/sails

thanks for the work!
now i have a issue with get populate count , let's say i have a many-to-many relationship like User--Pets , i want see a specific user's pets count without populate all the Pets.
is there some "count" like method or syntax can directly use on populate?

Most helpful comment

Is it available? so needed

All 21 comments

I think this would be a wonderful feature! Allow for a query that returns the number of associated records on a particular associated collection. I propose that we count by uniqueness on a field, i.e.,
Users.findOne(42).populate('files', {count: id})

I'm wondering if it wouldn't be simpler to make something like: Pets.count({ user: 42 }). I don't think this is currently supported but it would be a nice way of going about it.

For one-to-many relationships, you could certainly go about it that way: Pets.find({user: 42}).count() returns the number of pets associated with user 42. What I'm thinking of would be the ability to return the count _on a resulting record_, possibly in lieu of the associated records themselves. Ex:

Users.findOne(42).populate('pets', {count: id})
.exec(function(err, user) {

    // Some non-negative integer
    var petCount = user.pets;

    // This still works
    user.pets.add(2);
})

Hello there,
what about the case when you need to get several count values at once on a search?
I'm trying to find a way to remove the ugly count it the following exemple. The goal here is to get the number of pets every owner named "John" has.

ownersdb.find({firstname: "john"}).populate('pets')
    .exec(function(err, owners) {

       for (var i in owners) {
            owners[i].uglycount = owners[i].pets.length;
        }

        callback(err, owners);
});

Any idea how to deal nicely with that?

@devinivy take a look at balderdashy/sails#1951 (@blah238 referenced)

I liked where @aclave1 was headed and the thread died on the vine.

Keeping a count attribute name different from the actual attribute name would help with presenting the result with sailsjs as a RESTful API. If pets is always an array and something like petsCount is always a number there is no ambiguity for any downstream systems looking for a strict type.

@kmcgrath I think that's a great solution.

@aclave1 suggested,

User
  .findOne({name:'kevin'})
  .populateCount({'followersCount':'followers'})
  .then(function(results){
     console.log(result);//{name:'kevin','followersCount':100}
   });

All I would do is suggest that the key/value of the object be swapped: populateCount({'followers': 'followersCount'}) reading "populate the count of followers as followersCount"

Thanks for posting, @mydearxym. I'm a repo bot-- nice to meet you!

It has been 60 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

  • review our contribution guide to make sure this submission meets our criteria (only _verified bugs_ with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

Thanks so much for your help!

It does sounds like a good solution. Any news on this?

Reopening until this is PRed into the roadmap.

Thank-you @devinivy!

Thanks for posting, @mydearxym. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

  • review our contribution guide to make sure this submission meets our criteria (only _verified bugs_ with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

Thanks so much for your help!

Added to the roadmap. It's available if anyone would like to own the feature.

I want to count user followers, like this question from SO. But it did not work for me.
What exact query syntax is? Thank you!

User.count().where({follower: followerId})
.exec(function(err, numberOfFollowings) {
  //numberOfFollowings will be the integer that you need
})

Ref: http://stackoverflow.com/a/28180438/1896897

I would really love to see this added.

This will be super helpful.

is this feature available now

+1

This is a much needed feature in real world applications. Is it available now ??? I see this topic is more than 3 years. so any progress ??

Is it available? so needed

Do we have any news on this ?

Was this page helpful?
0 / 5 - 0 ratings