0

I'm facing problem with getting data from multiple references. This data is required to build one of my objects. I would like to retrieve them at the "same" time like batch or transaction. Even if my document contains subcollection i have to get this subollection in separate query. I know that i can nested my queries but I'm not sure if this is good approach.

Could you share some advise please?

Scorps
  • 31
  • 1
  • 8

2 Answers2

1

In Firestore, it's not possible to query across collections with different names, and it's not possible to fetch multiple individual documents at the same time. If you have multiple references, it requires multiple queries to get all the data.

It's not clear from you question what you ought to do specifically, but in any case, it's going to require multiple queries.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I have Collection of "Users" but in User document i have another collection which i need to fill my POJO class. – Scorps Jul 06 '20 at 00:56
  • Yeah, you're going to need multiple queries. – Doug Stevenson Jul 06 '20 at 00:59
  • I was thinking about to use (in my MVVM) MediatorLiveData to make sure that my data is complete and ready for user. What do you think about it? Maybe there is other way? – Scorps Jul 06 '20 at 01:04
  • It doesn't really matter what vehicle you use to get the data across, it's going to come from multiple queries. – Doug Stevenson Jul 06 '20 at 03:10
1

As @Doug Stevenson just said,

it's not possible to query across collections with different names, and it's not possible to fetch multiple individual documents at the same time

What I have done to face this issue is to create a cloud functions that can make all the queries I need. Once the cloud functoin gets the result of each query, the object is built and send back as a response. That way, the android application only makes a call to one service and the cloud function handles the multiple querying. Hope that helps

Mateo Hervas
  • 545
  • 1
  • 5
  • 20
  • Yes, i was quite aware that its directly impossible, and i was even thinking about solution that you just provided (Thank you for that :) ), but i wasn't sure if there is another (simpler) way to do it. – Scorps Jul 06 '20 at 00:54