0
0
Firebasecloud~10 mins

Data aggregation patterns 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 get a reference to the Firestore collection named 'orders'.

Firebase
const ordersRef = firestore.[1]('orders');
Drag options to blanks, or click blank then click option'
Acollection
Bdoc
Cget
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'doc' instead of 'collection' to get a collection reference.
Using 'get' or 'set' which are for reading or writing data, not referencing.
2fill in blank
medium

Complete the code to increment the 'totalSales' field atomically in a Firestore document.

Firebase
docRef.update({ totalSales: firebase.firestore.FieldValue.[1](1) });
Drag options to blanks, or click blank then click option'
Aincrement
Bdelete
CarrayUnion
DserverTimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'delete' which removes a field.
Using 'serverTimestamp' which sets the time, not increments.
3fill in blank
hard

Fix the error in the code to correctly aggregate the sum of 'amount' fields from multiple documents.

Firebase
let total = 0;
docs.forEach(doc => {
  total [1] doc.data().amount;
});
Drag options to blanks, or click blank then click option'
A=
B*=
C-=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which resets total instead of adding.
Using '-=' or '*=' which change the value incorrectly.
4fill in blank
hard

Fill both blanks to create a Firestore query that gets documents where 'status' is 'completed' and orders by 'date' descending.

Firebase
const query = collectionRef.where('status', '==', [1]).[2]('date', 'desc');
Drag options to blanks, or click blank then click option'
A'completed'
B'pending'
CorderBy
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pending' instead of 'completed' for the filter.
Using 'limit' instead of 'orderBy' for sorting.
5fill in blank
hard

Fill all three blanks to create a map of user IDs to their total order amounts, filtering orders above 100.

Firebase
const userTotals = {};
orders.forEach(order => {
  if (order.amount [1] 100) {
    const userId = order.[2];
    userTotals[userId] = (userTotals[userId] || 0) [3] order.amount;
  }
});
Drag options to blanks, or click blank then click option'
A>
BuserId
C+=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for filtering.
Accessing wrong property instead of 'userId'.
Using '=' instead of '+=' for accumulation.