0
0
Firebasecloud~10 mins

Optimistic concurrency 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 update a Firestore document only if it exists.

Firebase
const docRef = firestore.collection('users').doc('user1');
docRef.[1]({ age: 30 })
Drag options to blanks, or click blank then click option'
Aset
Bget
Cdelete
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using set() which overwrites or creates the document.
Using get() which only reads data.
Using delete() which removes the document.
2fill in blank
medium

Complete the code to perform a transaction that reads and updates a Firestore document atomically.

Firebase
firestore.runTransaction(async (transaction) => {
  const doc = await transaction.[1](docRef);
  const newAge = doc.data().age + 1;
  transaction.update(docRef, { age: newAge });
});
Drag options to blanks, or click blank then click option'
Aset
Bget
Cupdate
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using update() directly without reading the document.
Using set() which overwrites data.
Using delete() which removes the document.
3fill in blank
hard

Fix the error in the transaction code to ensure the update only happens if the document exists.

Firebase
firestore.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  if (doc.exists) {
    transaction.[1](docRef, { age: doc.data().age + 1 });
  }
});
Drag options to blanks, or click blank then click option'
Aupdate
Bset
Cget
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using set() which overwrites or creates documents.
Using get() which only reads data.
Using delete() which removes the document.
4fill in blank
hard

Fill both blanks to implement optimistic concurrency control using Firestore document versioning.

Firebase
firestore.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  const currentVersion = doc.data().[1];
  if (currentVersion === expectedVersion) {
    transaction.update(docRef, { age: newAge, [2]: currentVersion + 1 });
  } else {
    throw new Error('Version mismatch');
  }
});
Drag options to blanks, or click blank then click option'
Aversion
Bage
Ctimestamp
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated fields like age or timestamp for version control.
Not incrementing the version after update.
5fill in blank
hard

Fill all three blanks to implement a Firestore transaction that safely increments a counter with optimistic concurrency.

Firebase
firestore.runTransaction(async (transaction) => {
  const doc = await transaction.get(docRef);
  const currentCount = doc.data().[1];
  const currentVersion = doc.data().[2];
  if (currentVersion === expectedVersion) {
    transaction.update(docRef, { [1]: currentCount + 1, [2]: currentVersion + 1 });
  } else {
    throw new Error('Version conflict');
  }
});
Drag options to blanks, or click blank then click option'
Acount
Bversion
Cage
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names for count and version.
Not updating the version field after increment.