![]() |
VOOZH | about |
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 CloudFirestore. 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
Firebase provides two main databases for storing data: Realtime Database shows the and Cloud Firestore are explained below:
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.
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.
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.
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.
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