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

Time:12-05

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?

CodePudding user response:

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.

  • Related