Using Firestore database - Firebase Basics Series - Part 7
Tags: firebase, tutorial
Published: 2021-06-18 18:04:19

<a href="https://www.buymeacoffee.com/areeburrub" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>

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](https://firebase.google.com/docs/database/rtdb-vs-firestore?authuser=0#key_considerations)

## Create Database
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfngd2xxy4xgnnjg9t0k.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kwuu2e05c49qc6s6fyz.png) 
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](https://firebase.google.com/docs/hosting/reserved-urls#libraries_hosting-urls)
```
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-firestore.js"></script>
```
## Using Firestore
First we have to initialize the firestore in order to use it
```
const db = firebase.firestore();
```
### 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');
```
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
```

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
```

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](https://firebase.google.com/docs/firestore/data-model#web-v8)

### Adding, Removing and Updating
it's very much similar to Realtime db
- To Add data
```
db.collection('nameOfColl').add({name : 'AREEB'})
```

- To Delete data
```
db.collection('nameOfColl').doc(uniqueId).delete();
```

- To Update data
```
db.collection('nameOfColl').doc(uniqueId).update({
	fruite : 'PineApple',
	name : 'Areeb ur Rub'
});
```

### 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 
{% user areeburrub %}

<a href="https://www.buymeacoffee.com/areeburrub" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>