Cloud Firestore is a NoSQL, scalable, and serverless cloud database offered by Google Cloud Platform. It’s designed to store and sync data in real-time, making it an excellent choice for building web and mobile applications. This cheatsheet provides a quick reference guide to common operations in Cloud Firestore, along with code snippets to help you get started.
1. Initialize Firestore
To use Cloud Firestore in your application, you need to initialize it first.
// Import the Firestore module
const admin = require('firebase-admin');
// Initialize Firestore
admin.initializeApp();
const db = admin.firestore();
2. Add Data
Adding data to Firestore is straightforward. Use the add
method to insert a new document into a collection.
// Add data to a collection
const data = {
name: 'John Doe',
age: 25,
city: 'New York',
};
const docRef = db.collection('users').add(data);
console.log('Document added with ID: ', docRef.id);
3. Read Data
Reading data from Firestore involves querying the database. Use the get
method to retrieve documents from a collection.
// Read data from a collection
const usersRef = db.collection('users');
const snapshot = await usersRef.get();
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
});
4. Update Data
Updating data in Firestore can be done using the update
method.
// Update data in a document
const docRef = db.collection('users').doc('userID');
// Update the 'age' field to 26
await docRef.update({
age: 26
});
5. Delete Data
Deleting a document is simple using the delete
method.
// Delete a document
const docRef = db.collection('users').doc('userID');
await docRef.delete();
6. Query Data
You can filter and sort data using queries.
// Query data with conditions
const query = usersRef.where('age', '>', 21).orderBy('age', 'desc');
const snapshot = await query.get();
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
});
7. Real-time Updates
Firestore provides real-time updates through snapshots.
// Real-time updates
const docRef = db.collection('users').doc('userID');
docRef.onSnapshot((doc) => {
console.log('Current data: ', doc.data());
});
8. Batched Writes
Perform multiple write operations as a single batch for atomicity.
// Batched writes
const batch = db.batch();
const docRef1 = db.collection('users').doc('user1');
batch.set(docRef1, { name: 'Alice' });
const docRef2 = db.collection('users').doc('user2');
batch.set(docRef2, { name: 'Bob' });
await batch.commit();
This cheatsheet provides a quick reference for common Cloud Firestore operations. Remember to tailor the code snippets to fit your specific application requirements. For more detailed information, refer to the official Cloud Firestore documentation.
FAQ
1. What is Cloud Firestore?
Cloud Firestore is a NoSQL, serverless database provided by Google Cloud Platform. It enables real-time data synchronization and is commonly used for building web and mobile applications.
2. How do I initialize Cloud Firestore in my application?
To initialize Cloud Firestore, import the Firestore module and use the initializeApp
method. Example:const admin = require('firebase-admin'); admin.initializeApp(); const db = admin.firestore();
3. Can I perform real-time updates with Cloud Firestore?
Yes, Cloud Firestore provides real-time updates through snapshots. You can use the onSnapshot
method to listen for changes in your data in real-time.
4. What is the purpose of batched writes in Cloud Firestore?
Batched writes in Cloud Firestore allow you to perform multiple write operations as a single atomic batch. This ensures that either all operations succeed or fail together, providing data consistency
5. How can I query data in Cloud Firestore?
You can query data in Cloud Firestore using the where
method to apply conditions and the orderBy
method to sort the results. Example:const query = usersRef.where('age', '>', 21).orderBy('age', 'desc'); const snapshot = await query.get();