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

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