Table of contents
1.
Introduction
2.
Firestore
3.
The Data Model in Cloud Firestore
3.1.
Documents
3.2.
Collections
4.
How to Add / Update Data in Firestore?
4.1.
Set a Document
4.2.
Add a Document
5.
How to Fetch Data in Firestore?
6.
How to Query for Data in Firestore?
6.1.
Operators
7.
Frequently Asked Questions
7.1.
What is Firebase?
7.2.
Is Firestore a SQL or NoSQL database?
7.3.
Describe the methods used to add / update data in Firestore.
7.4.
Why do we use await keyword?
8.
Conclusion
Last Updated: Aug 13, 2025
Easy

Modifying Data in Firestore

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas, 

You must have heard about Firebase. Firebase provides two cloud-based database solutions which can be used for real-time data syncing. One of them is Firebase’s original database, i.e., Realtime Database. It is a low-latency and efficient mobile app database, requiring real-time syncing among the states. Second is the newest database offered by Firebase, i.e., Cloud Firestore, which offers richer features and faster queries. 

Introduction

In this article, we will discuss the Firebase Firestore and the methods by which we can modify data in Firestore. So, stay connected, and let's start with learning about Firestore.

Also See, Interpolation in Angular

Recommended Topic, Cognizant Eligibility Criteria

Firestore

Firestore is a NoSQL, flexible, and scalable document database built for web, mobile, and server development from Google Cloud and Firebase. It is made for high performance, automatic scaling, and ease of application development.

Firestore

It works like Firebase Realtime Database to keep our data in-sync across the applications to offer offline support for web and mobile that can work regardless of Internet connectivity or network latency.

Before moving forward, let’s understand how data is stored in Firestore.

The Data Model in Cloud Firestore

Firestore is a document-oriented database that stores the data in the form of documents arranged in collections; there are no rows or tables in Firestore. These documents can contain nested objects and subcollections. We simply assign the data to this document; if it doesn’t exist, firebase will create it. 

Data Model

Let’s discuss documents and collections in detail:

Documents

The unit of storage in Firestore is the document. It contains fields mapped with the values. A name is given to each document which helps in identifying them. Below is an example of how fields are present in the document:

firstName: “Kartik”
lastName: “Kukreja”
course: “Btech”

 

Nested objects can also be present. For example,

name:
	first: “Kartik”
	last: “Kukreja”

Collections

Collections are the containers for documents, They can’t contain the raw fields and values directly, and they can’t contain other collections. For example, To store the data of students in a class, We can use a user's collection, in which every student’s data can be represented in a document.

Now let's see how we can add or update data in Firestore.

How to Add / Update Data in Firestore?

There exist multiple ways to add data in Firestore. Let’s discuss them one by one:

👉 First, we can create an empty document containing an automatically generated identifier, and then we can later assign data.

👉 We can also add a new document to an existing collection, and Firestore will automatically generate the identifier for the document.

👉 We can set the data of a document and then specify its identifier.

 

Let’s discuss one by one how we can add, update or set individual documents in Web Version 9.

Set a Document

The set () method is used to create documents. We can create a new document or overwrite an existing document.

First, let's see how to create a new document:

import { doc, setDoc } from "firebase/firestore";

// Use setDoc() to add a new document
await setDoc(doc(db, "users", "University"), {
  name: "Kartik Kukreja",
  course: "Btech",
  State: "Haryana",
  City: "Faridabad"
});
You can also try this code with Online Javascript Compiler
Run Code

 

The data will be stored in the document like this:

output

If a document already exists, we can merge the data into it. We need to pass the option of merge with value true as shown in the given example:

import { doc, setDoc } from "firebase/firestore";

const userRef = doc(db, 'users', 'University');
setDoc(userRef, { capital: true }, { merge: true });
You can also try this code with Online Javascript Compiler
Run Code

Add a Document

As seen in the above example, we must specify an ID whenever we create a document; Firebase can also auto-generate an ID if we don’t provide which can be done by the addDoc() method.

import { collection, addDoc } from "firebase/firestore";

// use addDoc to create a document with a generated ID
const docRef = await addDoc(collection(db, 'users'), {
  name: "Kartik",
  course: "Btech"
});

// Access the document ID using docRef.id
console.log("ID generated for the document is: ", docRef.id);
You can also try this code with Online Javascript Compiler
Run Code

 

The ID will be auto-generated as shown below:

output


Update a Document

We have seen how to merge or overwrite documents, but what if we want to update some fields only instead of overwriting? This can be done using the update() method. Let’s look at an example:

import { doc, updateDoc } from "firebase/firestore";

const userRef = doc(db, "users", "University");
await setDoc(userRef, {
    name: "Kartik",
    course: "Btech",
    state: "Haryana"
});

// To update course and state, use updateDoc
await updateDoc(frankDocRef, {
    "course": "BCA",
    "state": "Punjab"
});
You can also try this code with Online Javascript Compiler
Run Code

 

Before updating, the document looks like this:

output


The fields that are provided in updateDoc() will be updated:

output

How to Fetch Data in Firestore?

Now let's look at how we can retrieve data from Firestore. There are three ways by which we can retrieve the data stored in Firebase Firestore. They can be used with collections of documents, documents, or the results of queries. Check out these three methods:

👉 To get the data once, we can call a method.

👉 We can set a listener to accept data-change events.

👉 Use an external source to bulk-load snapshot data in Firestore.

 

To fetch data of a single document in Firestore, we use the get() method:

import { doc, getDoc } from "firebase/firestore";

const userRef = doc(db, "users", "University");
const docSnap = await getDoc(userRef);

if (docSnap.exists()) {
    // If the document snap exists, print data with the help of docSnap.data()
  console.log("Document data:", docSnap.data());
} else {
    // Document snap doesn't exist in this case
  console.log("No such document!");
}
You can also try this code with Online Javascript Compiler
Run Code

 

The getDoc() method returns a promise; we can assign it to another constant, usually docSnap ( Document Snapshot ). To access the data, we use docSnap.data() method.

Learn more about Introduction to C# here.

How to Query for Data in Firestore?

Firestore offers a bunch of powerful query functionality that specify the documents from a collection or group of collections. To create a query, we can use the query() method, and we can use get() to retrieve the results. Let’s discuss this with an example:

import { collection, query, where, getDocs } from "firebase/firestore";

// Create a query using query() method
const que = query(collection(db, "users"), where("state", "==", "haryana"));

// Use getDocs() to get the querySnapshot
const querySnapshot = await getDocs(que);

// Print all queries
querySnapshot.forEach((doc) => {
  // Print the results using doc. data()
  console.log(doc.id, " => ", doc.data());
});
You can also try this code with Online Javascript Compiler
Run Code

 

First, create a query with the query() method and generate a snapshot of the query with the use of getDocs(). Traverse each document from this snapshot and access the data using doc.data().

In the above example, we have used == in query; it is called equal to operator. There are many other operators; let’s look at them.

Operators

Operators are provided as a parameter in the where() method. It takes three parameters: a field according to which you want to filter, an operator for comparisons, and a value to compare. The following are the operators that Firestore supports:

🔶 < less than: Less than operator returns the documents where the given field is less than the comparison value.

🔶 <= less than or equal to: It returns the documents where the given field is smaller than or equal to the comparison value.

🔶 == equal to: This operator returns the documents in which the given field matches the comparison value.

🔶 > greater than: Greater than operator returns the documents where the given field is larger than the comparison value.

🔶 >= greater than or equal to: It returns the documents where the given field is larger than or equal to the comparison value.

🔶 != not equal to: Not equal to (!=) operator returns the documents where the given field does not matches the comparison value.

🔶 array-contains: It is used to filter the documents based on the array values. 

🔶 array-contains-any: array-contains-any operator is used to find documents where the field in the array contains one or more than one comparison value. Ten array-contains can be combined using this.

🔶 in: in operator is used to return documents where any equality (==) conditions matches; up to 10 equality clauses can be combined.

🔶 not-in: This operator is used to return documents where the given field does not match any of the comparisons value. Ten not-equal (!=) clauses can be applied to the same field.

Frequently Asked Questions

What is Firebase?

Firebase provides cloud services and tools which assist web and mobile developers in resolving issues through the application lifecycle. It enhances usability and boosts user engagement.

Is Firestore a SQL or NoSQL database?

Firestore is a NoSQL, flexible, and scalable document database for high performance, automatic scaling, and ease of application development. Unline SQL database, data is not stored in the form of rows or tables; data is stored in the form of documents.

Describe the methods used to add / update data in Firestore.

We can use the set() method to create documents and the addDoc() method to add the document. We can update the data by overwriting or merging the documents, which can be done using the updateDoc() method.

Why do we use await keyword?

Whenever a function sends a response as a promise, we use await keyword. It pauses the execution of the surrounding async function and resumes when the promise is settled, i.e., either fulfilled or rejected.

Conclusion

In this article, we have discussed Firestore and its benefits over Realtime databases and how data is stored in Firestore. We have also looked at how we can add, update, and fetch data in Firestore. Last, we discussed the queries and how they can be used to retrieve results.

If you want to learn more about Firebase, you can check this article: Firebase.

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow.

Happy Coding !!

Live masterclass