Writing Data in Firebase

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: 'john@example.com'
});

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'] = 'john_new@example.com';
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: 'john@example.com'
});

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: 'jane@example.com'
});

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