Codementor Events

Firebase firestore tricks

Published Oct 23, 2018

As a full-stack developer who worked numerous times using lots of databases specially firebase NoSql database i'm confident to state some nice tricks when storing your data into a firebase firestore repo.
A firestore consists of two major parts, a document and a collection. Each Document could contain a single collection and each collection also could contain another document, so you could basically stack docs inside of collections and so on.
When it comes to storing data into a noSQL database it is best said to duplicate the data you always tend to retreive alot. An example would be a User table that has Wallet inside of each user, the perfect way is to make a collection outside of Wallet and also to make a sub-collection of the Wallet inside each user, this could make your life much easier as querying would be simply by accessing the desired user wallet. Also now you can simply query wallets and aquire statistics on these wallets instead of iterating over users->wallets.
The only drawback here is when updating, you will simply update both the wallet inside of the user's collection and the wallet collection, a good practise is to give both the same ID so it would be straight forward to just update Wallet[ID-X] and User->Wallet[ID-X] that if there is an ID called ID-X.

cheers.

Discover and read more posts from Sekab
get started
post commentsBe the first to share your opinion
Show more replies