Complete the code to read data from Firestore only once.
const docRef = firestore.collection('users').doc('user1'); const doc = await docRef.[1]();
Using get() reads the document once, reducing read costs compared to onSnapshot() which listens continuously.
Complete the code to batch write multiple documents to reduce write operations.
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();
Using set() in batch writes multiple documents efficiently, reducing the number of separate write operations.
Fix the error in the Firestore query to limit reads and reduce costs.
const querySnapshot = await firestore.collection('products').[1]().limit(10).get();
Using orderBy('price') before limit(10) ensures the query returns the first 10 products sorted by price, reducing unnecessary reads.
Fill both blanks to create a Firestore query that fetches only active users with minimal reads.
const activeUsersQuery = firestore.collection('users').[1]('status', '==', [2]);
Using where('status', '==', 'active') filters users to only those active, reducing the number of documents read.
Fill all three blanks to create a Firestore listener that unsubscribes after first data fetch to reduce continuous reads.
const unsubscribe = firestore.collection('messages').onSnapshot(snapshot => { snapshot.docs.forEach(doc => console.log(doc.data())); [1](); }); // Later in code [2] = unsubscribe; [3]();
Calling unsubscribe() inside the listener stops further reads. Assigning it to a variable and calling that variable later also stops listening, reducing costs.