1

I am new to Firestore and trying to fetch the documents based on a condition. Below is the Firestore structure I am currently working on.

Firestore Schema

I am trying to fetch only those documents from CollectionA, in which one of the document of CollectionB has DataFieldB1 = "Google".
This would avoid the going through the CollectionA documents one by one.
Also when the number of documents are huge, this may not be an efficient way to fetch the documents.

Is this kind of multi-level queries are supported in firestore? Could you please guide me to find a solution for this problem.

Thank you

KK2491
  • 451
  • 1
  • 8
  • 25

1 Answers1

2

Queries in Firestore can only contain conditions on the documents they return. There is no way to query CollectionA on a condition in CollectionB. You'll have to duplicate (or aggregate) the information into the relevant documents in CollectionA too.

Alternatively, you can run a collection group query on all CollectionB documents, and then find the relevant parent documents. This is typically less efficient than duplicating the field, but it may be better in cases where you have very little duplication of the value you're filtering on.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your answer, I was about to update regarding the Collection Group, and saw your updated answer. – KK2491 Jun 28 '21 at 17:21
  • Could you please brief about the duplication? Apparently I have multiple documents inside ```CollectionB```, hence duplicating may not be feasible. Right? – KK2491 Jun 28 '21 at 17:22
  • If there's only one document in each `CollectionB` with the value that you look for, the result set of a collection group query will contains one document from a `CollectionB` for each document from `CollectionA`, which is quite efficient. But if there are multiple documents in each `CollectionB` for the same parent document, you end up reading more documents. Whether that is too many, is something only you can decide of course. – Frank van Puffelen Jun 28 '21 at 17:24
  • In my scenario, I think every ```CollectionA``` documentt will ideally have one ```CollectionB``` document once the query is executed. Originally one ```CollectionA``` document will have multiple ```CollectionB``` documents. – KK2491 Jun 28 '21 at 17:31
  • Yeah, so either may work for you. I typically prefer duplicating the data, but that may be personal. – Frank van Puffelen Jun 28 '21 at 18:11
  • Could you please give a reference to find the details on the duplication. I am bit confused about how to duplicate the data. – KK2491 Jun 29 '21 at 00:20
  • In some cases this is not a practical solutions. For example a discord like app. You can't store the members on a channel, because the number of members might grow too high, and you cannot realistically duplicate the data for each user because the data is updated too often. You can store the membership on the user but that's a lot more reads than needed. There is no efficient, real solution in this case that firestore can provide. – Ced Oct 26 '21 at 19:35