0
0
Firebasecloud~10 mins

Why atomic operations ensure consistency in Firebase - Test Your Understanding

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

Complete the code to perform an atomic update on a Firestore document.

Firebase
db.collection('users').doc('user1').[1]({ age: 30 });
Drag options to blanks, or click blank then click option'
Adelete
Bget
Cset
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using set() overwrites the whole document, not atomic update.
2fill in blank
medium

Complete the code to run a transaction that increments a counter atomically.

Firebase
db.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  const newCount = doc.data().count + [1];
  transaction.update(docRef, { count: newCount });
});
Drag options to blanks, or click blank then click option'
A1
B-1
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 does not change the count.
Using negative numbers decreases the count.
3fill in blank
hard

Fix the error in the transaction code to ensure atomicity.

Firebase
db.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  const newBalance = doc.data().balance [1] 100;
  transaction.update(docRef, { balance: newBalance });
});
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + increases balance incorrectly.
Using * or / changes balance unexpectedly.
4fill in blank
hard

Fill both blanks to atomically increment and check a value in Firestore.

Firebase
db.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  if (doc.data().stock [1] 0) {
    transaction.update(docRef, { stock: doc.data().stock [2] 1 });
  }
});
Drag options to blanks, or click blank then click option'
A>
B+
C-
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes wrong condition.
Using + instead of - increases stock incorrectly.
5fill in blank
hard

Fill all three blanks to atomically update multiple fields in a Firestore document.

Firebase
db.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  transaction.update(docRef, {
    balance: doc.data().balance [1] 50,
    points: doc.data().points [2] 10,
    status: [3]
  });
});
Drag options to blanks, or click blank then click option'
A+
B-
C'active'
D'inactive'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators changes values incorrectly.
Setting status to 'inactive' is not intended here.