Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase v9 – Get a Referentiated Document Content

I don’t understand why I’m not finding this documentation anywhere.

But I have a collection called users in my Firebase Firestore project. Inside users there are three collections: companies, policies and stores.

In the collection policies I have one store field and one company field that are references to one of that user’s store or company.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ok so as far as now is fine. But now, I’m performing the next query:

const subcollectionSnapshot = await getDocs(collection(db, 'users', 'S3casIyXxdddEAaa1YJL6UjBXLy2', 'policies'));

And the response is the next one:
enter image description here

But now… how can I get the company and store nested documents? How can I "populate" them in order to see their information and access their fields?

Thanks.

>Solution :

It looks like the company and store fields are of type DocumentReference.

In that case you can get a DocumentSnapshot of each of them by calling getDoc() with the DocumentReference:

subcollectionSnapshot.docs.forEach((policyDoc) => {
  const companyRef = policyDoc.data()["company"];
  const companyDoc = await getDoc(companyRef);
  const storeRef = policyDoc.data()["store"];
  const storeDoc = await getDoc(storeRef);
  ...
})

If you have multiple policy documents, you will need to do this for each of them. There is no concept of a server-side join in Firestore (nor in most other NoSQL databases).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading