/firebase-tutorials

How to update a document in Firestore?

Update your Firestore document with Firebase. Our guide walks you through setting up Firebase, initializing your project, and running a Node.js update script.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to update a document in Firestore?

 
Step 1: Set Up Firebase
 

Before you update a document in Firestore, ensure you have set up Firebase in your project. If you haven't, follow these basic steps:

  1. Go to the Firebase console, sign in with your Google account, and click on "Add project" to create a new Firebase project.
  2. Once your project is created, select it and navigate to the "Firestore Database" option in the left sidebar to enable Cloud Firestore.

 
Step 2: Set Up Your Environment
 

Make sure you have Node.js and npm installed on your system. Create a new directory for your project and run the following command to initialize it:

npm init -y

Install Firebase using npm:

npm install firebase

 
Step 3: Initialize Firebase in Your Project
 

Create an index.js file and add the following code to initialize Firebase in your project:


const firebase = require('firebase/app');
require('firebase/firestore');

const firebaseConfig = {
  apiKey: "YOUR_API_KEY",
  authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
  projectId: "YOUR_PROJECT_ID",
  storageBucket: "YOUR_PROJECT_ID.appspot.com",
  messagingSenderId: "YOUR_MESSAGING_SENDER\_ID",
  appId: "YOUR_APP_ID"
};

firebase.initializeApp(firebaseConfig);

const db = firebase.firestore();

Replace the placeholder values with your actual Firebase project configuration details, which can be found in your Firebase console under Project settings.

 
Step 4: Update a Document in Firestore
 

To update a document, you'll need the specific document ID and the collection that the document belongs to. Use the following structure to update a document:


const updateDocument = async () => {
  try {
    const docRef = db.collection('your-collection-name').doc('your-document-id');
    
    await docRef.update({
      field1: newValue1,
      field2: newValue2
    });

    console.log('Document successfully updated!');
  } catch (error) {
    console.error('Error updating document: ', error);
  }
};

updateDocument();

Replace 'your-collection-name' and 'your-document-id' with the appropriate values from your Firestore database. Replace field1, field2, newValue1, and newValue2 with the specific fields you want to update and their new values.

 
Step 5: Run Your Script
 

In your terminal, run the following command to execute your script and update the Firestore document:

node index.js

Check the Firestore console to confirm that your document has been updated successfully.

 

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022