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

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
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:
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.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.