Firebase which is an robust mobile and web application development platform by Google, offers developers two powerful databases for storing and synchronizing data: Realtime Database and Cloud Firestore. These databases cater to different needs, from real-time synchronization to structured querying and hierarchical data models.
In this article, we explore the key features and methods of these firebase databases for Writing Data in Firebase that providing insights into how they can be effectively utilized in our applications
Understanding Firebase Database
Firebase provides two main databases for storing data: Realtime Database shows the and Cloud Firestore are explained below:
Realtime Database
- The Realtime Database is a NoSQL cloud database that stores data as JSON (JavaScript Object Notation). It is designed to offer real-time synchronization, meaning that changes to the data are propagated to every connected client in real time.
- The real-time database can handle many concurrent connections and scale to meet the demands of our application.
Cloud Firestore
- Cloud Firestore is a flexible and scalable NoSQL database for mobile, web, and server development. It is part of the Firebase platform and stores data in documents and collections.
- Unlike the Realtime Database, Cloud Firestore offers more structured querying and hierarchical data models,
Writing Data in Realtime Database
Method 1: Using set() Method
- The set() method in a real-time database overwrites data at the specified path.
- If the data doesn't exist, it creates it. This method is commonly used to set initial data or to completely overwrite existing data.
Example: Using set() Method
var firebase = require('firebase');
// Initialize Firebase app
var config = {
// Your Firebase config here
};
firebase.initializeApp(config);
// Get a reference to the database service
var database = firebase.database();
// Set data at a specific path
database.ref('users/1').set({
username: 'john_doe',
email: '[email protected]'
});
Output
Writing data to the Realtime Database under the path `/users/1`.
Executing the above code will write data to the Realtime Database under the path /users/1.
Method 2: Using update() Method
- The update() method in Realtime Database updates the specified data without overwriting existing data.
- It allows us to update multiple attributes of a node simultaneously.
Example: Using update() Method
var updates = {};
updates['/users/1/email'] = '[email protected]';
updates['/users/1/age'] = 30;
database.ref().update(updates);
Output
Updating the email and age of the user with ID 1 in the Realtime Database.
Executing the above code will update the email and age of the user with ID 1 in the Realtime Database.
Writing Data in Cloud Firestore
Method 1: Using set() Method
- In Cloud Firestore, the set() method is used to create or overwrite a single document.
- If the document doesn't exist, it will be created. If it does exist, it will be overwritten with the new data.
Example: Using set() Method
var firestore = firebase.firestore();
// Add a new document with a specified ID
firestore.collection('users').doc('user1').set({
name: 'John Doe',
age: 25,
email: '[email protected]'
});
Output
Creating or overwriting the document with the ID 'user1' in the 'users' collection in Cloud Firestore.
Executing the above code will create or overwrite the document with the ID 'user1' in the 'users' collection in Cloud Firestore.
Method 2: Using add() Method
- The add() method in Cloud Firestore is used to add a new document with an automatically generated ID.
- It's suitable for creating documents without specifying a document ID.
Example: Using add() Method
firestore.collection('users').add({
name: 'Jane Smith',
age: 30,
email: '[email protected]'
});
Output
Adding a new document with an auto-generated ID to the 'users' collection in Cloud Firestore.
Executing the above code will add a new document with an auto-generated ID to the 'users' collection in Cloud Firestore.
Conclusion
Firebase's Realtime Database and Cloud Firestore are good tools for modern app development which offering real-time synchronization and scalable data storage solutions. While Realtime Database effective in real-time updates and simple data structures, Cloud Firestore provides more advanced querying capabilities and hierarchical data models. By understanding these databases and their methods, developers can build robust and efficient applications
Similar Reads
Reading Data in Firebase
Firebase a comprehensive platform for building mobile and web applications, provides powerful tools for reading and managing data. Understanding how to read data from Firebase databases is essential for developers working with Firebase. In this article, we will explore the concepts, methods, and exa
3 min read
Firebase Realtime Database
Firebase Realtime Database is a powerful NoSQL cloud database that enables real-time data storage and synchronization across all clients. It's particularly suited for applications requiring live updates, such as chat apps and collaborative tools. By following the setup instructions and using the pro
7 min read
Firebase - Introduction
Firebase, a product of Google, is a powerful platform that enables developers to build, manage, and scale applications with ease. It simplifies app development by offering a secure and efficient backend, eliminating the need for server-side programming. Firebase supports multiple platforms, includin
7 min read
Writing and Reading Data in Cloud Firestore
Cloud Firestore is a NoSQL document database built for automatic scaling, high performance, and ease of application development. In this article, we will explore how to write and read data in Cloud Firestore along with complete with detailed examples and. Whether you are a beginner or looking to ref
8 min read
What Is Firebase?
Firebase is the platform developed by Google for building and managing web and mobile applications. It can provide developers with various tools and services that are designed to simplify the process of app development. It allows them to focus more on creating features and less on managing the infra
5 min read
Firebase Realtime Database: Reading and Writing Data
Firebase Realtime Database, a cloud-hosted NoSQL database developed by Google, provides a robust solution for achieving seamless real-time data updates across connected clients. In this article, We will learn about the Firebase Realtime Database, How to Setting Up the Firebase Realtime Database, wri
7 min read
Firebase Cloud Function
Firebase Cloud Functions which is a key service within Google's Firebase platform. It allows the developers to execute backend code responding to various events, such as Firebase services events or HTTPS calls. This capability enables developers to extend their applications' functionality without th
4 min read
Tracking User Engagement in Firebase
Understanding how users interact with our app is important for optimizing user experience and driving engagement. Firebase offers tools to track and analyze user engagement and help developers make data-driven decisions to improve their apps. In In this article, We will go through the process of tra
6 min read
Data Organization in Firebase Realtime Database
Firebase Realtime Database is a powerful tool that allows us to store and synchronize data in real-time across all clients. However, organizing data effectively in Firebase Realtime Database can be challenging for beginners. Proper data organization is important for efficient data retrieval, minimiz
7 min read
What is Firebase Authentication
Firebase Authentication is a powerful backend service offered by Google Firebase, designed to speed up the user authentication process in applications. Supporting various authentication methods, such as email/password, phone number, and social logins, Firebase Authentication ensures secure user auth
4 min read