DEV Community

Cover image for Using Firestore database - Firebase Basics Series - Part 7
Areeb ur Rub
Areeb ur Rub

Posted on • Updated on

Using Firestore database - Firebase Basics Series - Part 7

Buy Me A Coffee

Till now we have already used Firebase Realtime Database and Firebase Authentication, now in this post I will tell you about Firestore.

While Firestore and Realtime databases are very similar, Firestore maintains data in a more structured manner and can also do more complex querying.

To choose between both you can take help of firebase docs as it gives you choices to choose and will tell you which one best fits in your need category, visit Firebase Docs/rtdb-vs-firestore

Create Database

image
You can go to the firestore section from your console and create a database that will be in test mode for now, but we will set rules later on.

Project to use it in

I like project based learning a lot so we will make another website, where user can login and upload pictures and other can see it.
This project will let us work with firestore and firebase file storage.
image
I made this layout for the website you can use your creativity for doing so.

Adding Firestore to project

First we have to add the CDN script of firestore from Firebase Docs

<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js"></script>
Enter fullscreen mode Exit fullscreen mode

Using Firestore

First we have to initialize the firestore in order to use it

const db = firebase.firestore();
Enter fullscreen mode Exit fullscreen mode

Structure of database in firestore

Like .ref() in Realtime Db, firestore have .collection().
A collection can be of anything and a database can have many collection and each collection have documents.

A document is the place where all data is stored in a JSON like format.

Here's an example,

var areeb = db.collection('users').doc('areeburrub');
Enter fullscreen mode Exit fullscreen mode

in this the variable areeb will contain all of my information stored in my user doc and can be accessed like JSON file.

Structuring the document totally depends on us it can be a simple one like this

first : Areeb"
last : "ur Rub"
born : 2002
Enter fullscreen mode Exit fullscreen mode

or can be nested and have much more data like this

name :
    first : "Areeb"
    last : ur Rub"
Social:
    Instagram : areeb.ur_rub
    Linkedin : areeburrub
born : 1815
Enter fullscreen mode Exit fullscreen mode

or A document which also have sub collection with there collection, if you are confused with structure of data in firestore then read this : firestore-Docs/data-model

Adding, Removing and Updating

it's very much similar to Realtime db

  • To Add data
db.collection('nameOfColl').add({name : 'AREEB'})
Enter fullscreen mode Exit fullscreen mode
  • To Delete data
db.collection('nameOfColl').doc(uniqueId).delete();
Enter fullscreen mode Exit fullscreen mode
  • To Update data
db.collection('nameOfColl').doc(uniqueId).update({
    fruite : 'PineApple',
    name : 'Areeb ur Rub'
});
Enter fullscreen mode Exit fullscreen mode

More Features of Firestore

For now I am not introducing you to more features of firestore we will see it in next part.

Follow me


Buy Me A Coffee

Top comments (0)