user8583580
user8583580

Reputation:

Cloud Firestore database structuring

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:

Upvotes: 4

Views: 1732

Answers (1)

Scarygami
Scarygami

Reputation: 15569

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.

Upvotes: 4

Related Questions