Reference data type in Firebase’s Firestore.

Tram Ho

Hi everyone, this time I have a task related to database storage on firebase firestore. While researching on how to design database on Firestore, I found a good one of this database, so I wanted to write a short share. Let’s start!!!

First, let’s take a look at Firestore’s definition, “flexible, scalable NoSQL cloud database to store and sync data for client- and server-side development”.

From the definition, we can see Firestore is a NoSQL database, ie the database has no relationship, similar to MongoDB . A table in Firestore is a collection and a record is a document. Firebase structure the database as a file system, so if you want to see the document with id doc of collection col then you just need to enter /col/doc into console.

From the way the data is structured in the form of a file system, firebase generates a data type – Reference. This data type works quite similar to Foreign Key strong Relational Datatbase. For example, you have a League table that stores league information and a Club stores club information. Normally, we will save the league_id field in the club table. To find out which club has id 1 playing, you will need to use 2 queries.

However, if instead of saving league_id to save information of the league this club is playing, we will save that League’s refernce as leagueRef, then we only need 1 query to get the League that t need.

Simple and more explicit right.

To insert the reference data type is also very simple,

Thank you for reading my short article, hope it helps! I wish you a happy working day.

Share the news now

Source : Viblo