Sai Prashanth
Sai Prashanth

Reputation: 1915

How to query through multiple documents in a collection (Firestore/Flutter)

I have a function to get a user's tasks, I want to modify it to get all user's tasks. So what I want to do is to run my query through all documents in "tasksRef" but the .doc() takes only one parameter at a time

Below is the code I use to get a user's tasks:-

getTasks() async {
  QuerySnapshot snapshot = await tasksRef 
    .doc(currentUser.id)
    .collection('userTasks')
    .get();
}

Is there any way to do this?

Upvotes: 0

Views: 1173

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

If you want to query across all userTasks collections, you can use a collection group query:

getTasks() async {
  QuerySnapshot snapshot = await FirebaseFirestore.instance 
    .collectionGroup('userTasks')
    .get();
}

Keep in mind that this queries across all tasks for all users, so you'll typically want to add some conditions to the read operation to limit the number of results to something your application can handle.

Upvotes: 1

Related Questions