-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Comments
Related: #1951 |
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., |
I'm wondering if it wouldn't be simpler to make something like: |
For one-to-many relationships, you could certainly go about it that way: 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, 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 #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: |
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:
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:
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. User.count().where({follower: followerId})
.exec(function(err, numberOfFollowings) {
//numberOfFollowings will be the integer that you need
}) |
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 ? |
Hello guys |
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?
The text was updated successfully, but these errors were encountered: