0

In my application users can like certain posts. Their likes will be stored in this way.

Image of the database:

enter image description here

Basically: in the collection Posts -> postID -> collection Likes -> userID -> timestamp. What I want to get is the number of userIds within the Likes collection for all posts. Basically I want to display the number of posts with the highest likes first in my Recycler Adapter.

I believe I am looking for a query of the type that would help me get the number of currentUserIds and then display them in descending order in my adapter:

Query firstQuery = firebaseFirestore.collection("Likes").document(currentUserId)...
Alex Mamo
  • 112,184
  • 14
  • 139
  • 167

1 Answers1

0

What I want to get is the number of userIds within the Likes collection for all posts.

In this case, you should perform a Firestore collection group query:

A collection group consists of all collections with the same ID. By default, queries retrieve results from a single collection in your database. Use a collection group query to retrieve documents from a collection group instead of from a single collection.

So in your case, you should use the following query:

db.collectionGroup("Likes").get().addOnCompleteListener(/* ... */);

To actually count the number of documents, please check my answer from the following post:

Alex Mamo
  • 112,184
  • 14
  • 139
  • 167