I have collection users which have a sub collection called "notes about". "Notes about" have security rules that only users with role - "manager" can access them. Now everything works nice and so on, but my problem starts when manager decides to update his name, surname or imageUrl. Since all notes have also property called "addedBy" which contains user name, surname, id and imageUrl, I need to go through all users and all their notes about sub collection documents, so I can check each document and update it with cloud function. So far looks like I will have to refactor my db and store "notes about" in a separate collection. But maybe there is a way to get all collection documents with all their sub collection documents in another (reasonable) way. I would prefer not to refactor my DB but maybe that's the only solution at the moment. What are your thoughts?
-
Firestore reads are shallow, and only read from a single collection (or group of collections with the same name). See https://stackoverflow.com/q/55632268, https://stackoverflow.com/q/52457597, https://stackoverflow.com/q/46611279, https://stackoverflow.com/a/53267350 – Frank van Puffelen Sep 21 '19 at 14:35
1 Answers
But maybe there is a way to get all collection documents with all their sub collection documents in another (reasonable) way.
Unfortunately there isn't. Queries in Firestore are shallow, they only get items from the collection that the query is run against. There is no way to get documents from a top-level collection and other subcollections in a single query. Firestore doesn't support queries across different collections in one go. A single query may only use properties of documents in a single collection. IMO, you can go ahead by querying the database multiple times to get the desired values. Is not so slow as might think.
Another possible solution in your case might be to use collection group query. In order to use this new feature, all the subcollections shoould have the same name.
Now it's up to you to decide if you'll use more than one query to get the data, or use collection group query or even refactor the database to fit your needs.

- 130,605
- 17
- 163
- 193
-
Yeah, that's what I thought. The querying the database multiple time for me wouldn't be a problem, if the costs wouldn't go up with each db read and the more users I have the more expensive it could get. Anyway, Thanks for your answers! – ebem Sep 21 '19 at 14:20