4
votes

The Android sample app for Cloud Firestore has database structure as shown below:

enter image description here

Now consider a situation in which the first restaurant has very large number of ratings (Here ratings is a collection along with other documents in the first restaurant's id) and I just want to show basic details of all the Restaurants like name and city.

I would accomplish this by creating a reference as shown below:

mRestaurantRef = mFirestore.collection("restaurants").document(restaurantId);

I have following questions regarding this:

  • Is this the right way to do because I am getting a document snapshot which also includes ratings collection which I don't need for now as it can decrease the loading speed?
  • Should I change the firestore database structure as we do in firebase realtime database and flatten it?
1

1 Answers

4
votes

One of the main differences/advantages of Firestore over RTDB is that subcollections of a document won't be retrieved if you request a snapshot.

That means that only the basic details and not the ratings will be loaded, when you call mRestaurantRef.get()

You can afterwards retrieve the ratings (or a limited amount of the ratings) for the restaurant with a separate query.

You could also call mFirestore.collection("restaurants").get() (with where/orderBy/limit if necessary) to get a list of document snapshots for all the restaurants. Again only the basic data will be retrieved independent of any ratings.