Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create query syntax for populate counts #5598

Closed
mydearxym opened this issue Jan 28, 2015 · 22 comments
Closed

Create query syntax for populate counts #5598

mydearxym opened this issue Jan 28, 2015 · 22 comments

Comments

@mydearxym
Copy link

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?

@blah238
Copy link

blah238 commented Mar 23, 2015

Related: #1951

@devinivy
Copy link

devinivy commented Apr 2, 2015

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})

@devinivy devinivy changed the title populate count? Create query syntax for populate counts Apr 2, 2015
@dmarcelino
Copy link
Member

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.

@devinivy
Copy link

devinivy commented Apr 2, 2015

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);
})

@benjamintalou
Copy link

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?

@kmcgrath
Copy link

@devinivy take a look at #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.

@devinivy
Copy link

devinivy commented Jul 2, 2015

@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"

@sailsbot
Copy link

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!

@joaosa
Copy link
Contributor

joaosa commented Nov 6, 2015

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

@devinivy
Copy link

Reopening until this is PRed into the roadmap.

@devinivy devinivy reopened this Nov 30, 2015
@kmcgrath
Copy link

Thank-you @devinivy!

@sailsbot
Copy link

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!

particlebanana referenced this issue in balderdashy/waterline Dec 31, 2015
@particlebanana
Copy link
Contributor

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

ctartist621 referenced this issue in ctartist621/waterline Feb 3, 2016
@vunb
Copy link

vunb commented Jul 27, 2016

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

@Mikeysax
Copy link

Mikeysax commented Feb 9, 2017

I would really love to see this added.

@Sparshith
Copy link

This will be super helpful.

@gunarajesh
Copy link

is this feature available now

@AmarPrabhu
Copy link

+1

@prof-jagpreet
Copy link

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 ??

@Jaafar-abusair
Copy link

Is it available? so needed

@raqem raqem transferred this issue from balderdashy/waterline May 15, 2019
@abrantes01
Copy link

Do we have any news on this ?

@techwebdev
Copy link

Hello guys
any update on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests