0

I have a no. of users selecting a "category" of topic they like.

I'm trying to get the most popular category, since its dynamic.

In SQL, you could use something like ORDER BY value_occurrence.

This is how the database looks like:

enter image description here

How would I accomplish this using firestore?

Boron
  • 99
  • 10
  • 34
  • It's a bit unclear how you show the data. Do you store the count of people that selected a specific category already? It might also help if you show the documents that you're trying to query, and the code you're already tried. – Frank van Puffelen Apr 19 '21 at 14:56
  • I've added a sample image of the database structure – Boron Apr 20 '21 at 06:37

1 Answers1

1

You can create another collection for category, like you have users here, and then create a field in category for number of users liked and increase this field when ever anyone likes it. In this way you can easily get most liked category and it will be more effiecnt as when your data becomes large computing sum and arranging it in order will be harder

Shayan Samad
  • 294
  • 1
  • 10
  • This is a nice alternative solution, however the category is dynamic (its always changing and is also user defined) – Boron Apr 20 '21 at 08:37
  • 2
    @Boron This is the idiomatic way to solve aggregation queries on Firestore, as there's no built in feature for them. The fact that the categories are dynamic just means you'd dynamically add the categories to the new collection. For more on this, see https://stackoverflow.com/q/46554091 and the documentation on distributed counters: https://firebase.google.com/docs/firestore/solutions/counters – Frank van Puffelen Apr 20 '21 at 14:14
  • @Boron you can accept the answer if this helped you – Shayan Samad Apr 21 '21 at 04:16