Muhammad
Muhammad

Reputation: 2804

Is it possible to access a sub collections from parent collections (firebase cloud firestore)?

I have a collection like bellow:

db.collection("col1/doc1/col2/doc2/col3/doc3/col4").get();

Now I have to get all collection collection2 and collection4

With my current know knowledge I have to get them separately like bellow:

db.collection("col1/doc1/col2").get();

and

db.collection("col1/doc1/col2/doc2/col3/doc3/col4").get();

My expectation is I want to call it at once with collection2

db.collection("col1/doc1/col2").get().then(snapshot=>{
   //here something like snapshot.col3.col4
});

Is it possible like snapshot.col3.col4?

Why I want like this ?

Because I will not have to call it more than one time from server, may it increase performance and make it faster.

Upvotes: 0

Views: 240

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Is it possible like snapshot.col3.col4?

No, it's not. Queries in Firestore are shallow, which means that only get items from a collection that the query is run against. There is no way you can get documents from a top-level collection and a subcollections with different names in the same time. Firestore doesn't support queries across different collections with different names in one go. A single query may only use properties of documents in a single collection.

A possible solution for your issue might be the use of Collection group queries, but as the doc states, all collections should have the same name.

Another workaround might be, to create two separate queries and merge the result client side. For that I recommend you see my answer from the following post:

Upvotes: 2

Related Questions