How to count number of referenced Things

I have a simple Category Thing that represents categories such as Entertainment, Sports, Coaching, etc. with a single “name” (text) field. I’ve added a single Category field to User that a user configures. I want to be able to quickly obtain a sorted list by frequency of Category use (or reference by Users). I’d also like to obtain a random Category being used that has a non-zero number of uses. Is this easily possible in Bubble?

Hey there @ted

Imo you would need to create a field SearchedCategories (list of Categories) in the User data type.
Everytime User uses or references a Category, you add it to the User’s SearchedCategories.
To retrieve the count, you could then do Current User’s SearchedCategories:uniqueitem:sorted by count:firstitem. Have you tried this already?
To pick a random item in that list, you’d just need to do Current User’s SearchedCategories:uniqueitem:randomitem

Hope that helps
Ambroise

Need extra help?

Book a free 15 minutes call - happy to help if I can

@ambroisedlg Thank you, but I think there may be a miscommunication on my part. I’m not interested in the number of times a particular user references a category (only one selectable per user). I want to obtain the number of times “Sports”, say, was referenced by all users. Perhaps 5,123 users have selected Sports, so I want to know that the Sports category was selected by 5,123 users. I guess I can add a ref count by Category entry and increment/decrement but that’s prone to bugs if logic doesn’t also +/- when a user changes a category. That may be the only way to go?

Ah okay @ted I understand better thanks

Yes that’s what I would have suggested! I personally rarely have issues with this logic

Cheers
Ambroise

Need extra help?

Book a free 15 minutes call - happy to help if I can