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