Home > Software engineering >  Search Document from multiple collection using Flutter
Search Document from multiple collection using Flutter

Time:12-19

Can anyone tell me if it is possible to search multiple collections at once in Firebase? What I need to do is search the data in both the 2009 and 2010 collections in bio_data (more collections will need to be added in the future) and extract the document from one of them.

Or it doesn't matter if there is a way to get a name list of the collections inside a document

enter image description here

CodePudding user response:

Firestore does not have the concept of server-side joins or projections across collections. Each query or document read can only take data from a single collection, or from all collections that have the same name with collection group queries. If you need to load data from two collections, you'll need at least two reads.

For a better understanding I highly recommend watching the Get to know Cloud Firestore playlist.

CodePudding user response:

Michel's answer is correct.

In addition, I would suggest a change to your data model: Instead of having one sub-collection per year, you could have one unique sub-collection and add a year field to the docs in this unique collection.

This way it is easy to query by year: For one year, for several years with the in (up to 10 equality (==) clauses) or for all the years.


And with a Collection Group query you could even query all the data for all the students.

  • Related