I'm working on an Android app and I use Firestore to store my data. I have a collection of Users, and every user has a subcollection called Posts. Is there any way to retrieve all posts from all users to show them on my apps feed?
2 Answers
1
votes
Is there any way to retrieve all posts from all users to show them on my apps feed?
Sure, it is. You need to use collection group queries:
Use a collection group query to retrieve documents from a collection group instead of from a single collection. In code should look like this:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
rootRef.collectionGroup("Posts").get().addOnCompleteListener(/* ... */);
Using the above code, you'll be able to get all Post
objects that exist within all subcollections named Posts
.
0
votes
You will want to use a collection group query to get all the documents from all subcollections with the same name. For example:
db.collectionGroup("Posts").get()