Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve Firestore References

Firebase Firestore has a reference type while defining fields of a document which allows us to add a reference to another document via its "Document path".

For example, I have the document animals/3OYc0QTbGOTRkhXeiW0t, with a field name having value Zebra. I reference it in the array animals, of document zoo/xmo5wX0MLUEbfFJHvKq6. I am basically storing a list of animals in a zoo, by referring the animals to the corresponding animal document in the animals collections.

Now if I query a specific document from the zoo collection, will references to the animals be automatically resolved? Will I the get the animal names in the query result? If not, how can I achieve this?

like image 944
Abhilash Kishore Avatar asked Jan 25 '18 17:01

Abhilash Kishore


People also ask

What is the use of reference in firestore?

A reference provides full type-safe access to a Firestore Collection and Documents. The ODM provides a useful FirestoreBuilder widget which allows you to access your Firestore data via the ODM.

How do I use getDoc for firestore?

Firebase 9 Firestore Get A Document By ID Using getDoc() Let's get the first document of the cities collection by id. Import Firestore Database and de-structure the three methods that we need: getFirestore() → Firestore Database. doc() → It takes references of database, collection name and ID of a document as arguments.

When should you not use firestore?

If you have a very time-sensitive app, like online auctions where you can't have unpredictable delays (no one likes to lose because of something that we don't control), you shouldn't really use Firestore. You will have a hard time hiding the hiccups and you will only anger your users.

Why are my firestore reads so high?

Well, the answer is quite simple. In order to provide up-to-date data, the Firestore SDK needs to check the online version of the documents against the cached one. That's the reason why we are charged with new 50 reads, regardless of what exists in the cache or if something is changed or not on the server.


1 Answers

All document queries in Firestore are shallow, meaning that you only get one document in return for each document requested.

References in a document are not automatically fetched - you will have to make subsequent queries using the references in the document to get those other documents on your own.

Same thing with documents in subcollections - they require separate queries.

like image 66
Doug Stevenson Avatar answered Oct 10 '22 04:10

Doug Stevenson