Im working on a project using flutter and firebase, currently the database(firestore) has A collection named Projects
, each project has an owner(userId) and a subcollection named Sections
and each section has an Items
collection. Each Item has a list of tags (strings). I wanted to add a search Items by tag feature, but just realized that the nested collections structure makes it hard. Changing the database structure now would be a lot of work. Is there a way to apply a query to multiple subcollections? basically I would need to query all projects owned by the user then query for all in those projects sections and then all todos inside them that contain a certain tag.
I don`t want to do multiple queries and join them with frontend code because I'm using real time functionality and having to deal with multiple streams isn't ideal. Cloud functions aren't an option right now because I'm using the free plan.
Asked
Active
Viewed 58 times
1

PortalGamesMais
- 70
- 1
- 9
1 Answers
2
You're looking for a so-called collection group query, which searches across all collections with a certain name in one go.

Frank van Puffelen
- 565,676
- 79
- 828
- 807
-
Im reading the documentation in this link and I think i could use this for the tag part using `FirebaseFirestore.instance.collectionGroup("Todos").where("tags",arrayContains: tag)`. how can I do this and get only todos that are inside projects owned by the logged in user? – PortalGamesMais Nov 24 '22 at 01:59
-
This would work for that: https://stackoverflow.com/questions/68049541/collectiongroupquery-but-limit-search-to-subcollections-under-a-particular-docum/68049847#68049847 – Frank van Puffelen Nov 24 '22 at 02:04
-
So his Cities are like my projects and his attractions are like my Items. In the example he needed to query items for a specific project, I need to query Items for a list of projects. is it possible? – PortalGamesMais Nov 24 '22 at 02:14
-
Yup, seems like it should be. Give it a try and open a new question with a [minimal repro](http://stackoverflow.com/help/mcve) if you get stuck. – Frank van Puffelen Nov 24 '22 at 04:15
-
The projects wont necesseraly be consecutive, if I order by fieldpath I cant just start in one and end after another, I would have to start and stop for each project. – PortalGamesMais Nov 25 '22 at 00:19