Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore: how to reference another document?

I'm using Angularfire2. Suppose I have a users and comments collections. When adding a comment, what would be the correct way of referencing the user who created the comment? At first I thought I could simply create a comment with a structure like this:

{
    message: string,
    user: {
        uid: string,
        username: string,
        ...
    }
}

But then the problem is if the user updates his profile, then the data here would be incorrect. Is it possible to simply supply the user's id when creating the comment and get the entire user's data when querying the comments?

like image 811
Christian Avatar asked Jun 22 '26 20:06

Christian


1 Answers

Using firestore, you can save that field as a DocumentReference which is a ref to a document in another collection. This makes it so that field acts as it's own observable inside the 'Comment' observable. This article demonstrates how to set the reference path when setting or updating a document: https://groups.google.com/forum/m/#!topic/firebase-talk/UTgW7WwKFTU

Firestore offers a variety of data types explained here that are supported natively: https://firebase.google.com/docs/firestore/manage-data/data-types

I hope this can be of help!

like image 54
npesa92 Avatar answered Jun 25 '26 14:06

npesa92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!