7

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?

Abhilash Kishore
  • 2,063
  • 2
  • 15
  • 31

1 Answers1

7

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Is it possible to store the reference of the zoo with the animal? That way you could query all animals in a zoo (via the reference). This makes of course only sense when animals are unique (eg. Marty the Zebra) – Jürgen Brandstetter Jan 25 '18 at 17:25
  • You can't store whatever reference you want to any document within the same database. To know what all you can do with references, read this: https://stackoverflow.com/questions/46568850/what-is-firestore-reference-data-type-good-for – Doug Stevenson Jan 25 '18 at 18:28
  • Thanks, @DougStevenson, Please would you know if firebase has added the ability to automatically resolve subqueries. Because the firestore wouldn't just make any sense again using it like that. I'll rather stick to SQL Join tables. – Vixson Jun 16 '21 at 23:47