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 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

Similar Reads

Understanding Firebase Database

Firebase provides two main databases for storing data: Realtime Database shows the and Cloud Firestore are explained below:...

Writing Data in Realtime Database

Method 1: Using set() Method...

Writing Data in Cloud Firestore

Method 1: Using set() Method...

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...