0
0
Firebasecloud~10 mins

Cost optimization (read/write reduction) in Firebase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read data from Firestore only once.

Firebase
const docRef = firestore.collection('users').doc('user1');
const doc = await docRef.[1]();
Drag options to blanks, or click blank then click option'
Aget
BonSnapshot
Cset
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using onSnapshot causes continuous reads and higher costs.
Using set or update is for writing, not reading.
2fill in blank
medium

Complete the code to batch write multiple documents to reduce write operations.

Firebase
const batch = firestore.batch();
const doc1 = firestore.collection('orders').doc('order1');
batch.set(doc1, {status: 'pending'});
const doc2 = firestore.collection('orders').doc('order2');
batch.[1](doc2, {status: 'pending'});
await batch.commit();
Drag options to blanks, or click blank then click option'
Aset
Bupdate
Cdelete
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using get is for reading, not writing.
Using delete removes documents instead of writing.
3fill in blank
hard

Fix the error in the Firestore query to limit reads and reduce costs.

Firebase
const querySnapshot = await firestore.collection('products').[1]().limit(10).get();
Drag options to blanks, or click blank then click option'
Awhere('price', '>', 100)
Bget
CorderBy('price')
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Using get() here is incorrect because it is a method to execute the query, not build it.
Using limit() inside the chain twice causes errors.
4fill in blank
hard

Fill both blanks to create a Firestore query that fetches only active users with minimal reads.

Firebase
const activeUsersQuery = firestore.collection('users').[1]('status', '==', [2]);
Drag options to blanks, or click blank then click option'
Awhere
B'active'
C'inactive'
DorderBy
Attempts:
3 left
💡 Hint
Common Mistakes
Using orderBy instead of where does not filter results.
Using 'inactive' fetches unwanted users, increasing reads.
5fill in blank
hard

Fill all three blanks to create a Firestore listener that unsubscribes after first data fetch to reduce continuous reads.

Firebase
const unsubscribe = firestore.collection('messages').onSnapshot(snapshot => {
  snapshot.docs.forEach(doc => console.log(doc.data()));
  [1]();
});

// Later in code
[2] = unsubscribe;
[3]();
Drag options to blanks, or click blank then click option'
Aunsubscribe
Bconst stopListening
CstopListening
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Calling subscribe() is incorrect; onSnapshot already subscribes.
Not calling unsubscribe causes continuous reads and higher costs.