What Is Firebase Firestore 'Reference' Data Type Good For

What is Firebase Firestore 'Reference' data type good for?

References are very much like foreign keys.

The currently released SDKs cannot store references to other projects. Within a project, references can point to any other document in any other collection.

You can use references in queries like any other value: for filtering, ordering, and for paging (startAt/startAfter).

Unlike foreign keys in a SQL database, references are not useful for performing joins in a single query. You can use them for dependent lookups (which seem join like), but be careful because each hop will result in another round trip to the server.

What is the use case of reference type in Firestore Document and how to use it specially in an android app?

If you are storing the following document reference in the database:

/Users/OctUsers/1stWeekUsers/OlCvJFfWZeAlcttdlgzz/

And you want to get this:

FirebaseFirestore.getInstance().collection(collectionPath).document(documentPath).collection(collectionPath).document(documentPath)

You can symply rewrite it like this:

FirebaseFirestore.getInstance().document("/Users/OctUsers/1stWeekUsers/OlCvJFfWZeAlcttdlgzz/");

Remember, the most important part in a DocumenetReference object is the string path. If you want to get that String representation, use DocumentReference's getPath() method for that.

Furthermore, if you need to deserialize that path String back into a DocumentReference object, simply use FirebaseFirestore.getInstance().document(path).

How to get the referenced data from a Firebase Firestore reference?

If you have a DocumentReference field in your data, that is precisely the type that you'll get back when reading that data from Firestore. You'll need to call get() on that DocumentReference to get the data for the referenced document.

How to use the reference type efficiently to access a document in Cloud Firestore?

The value that the SDK gives you back for a DocumentReference field type in the database is actually a DocumentReference object from the SDK too. So you can just call get() on the value you get, to get a snapshot of the references document data.



Related Topics



Leave a reply



Submit