Bird
Raised Fist0
Firebasecloud~20 mins

Deleting fields in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Field Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
What happens when you delete a field in a Firestore document?

You have a Firestore document with fields name, age, and city. You run an update to delete the age field. What is the state of the document after this operation?

Firebase
const docRef = firestore.collection('users').doc('user1');
docRef.update({ age: firebase.firestore.FieldValue.delete() });
AThe <code>age</code> field is set to null but still exists in the document.
BThe <code>age</code> field is removed from the document; other fields remain unchanged.
CThe update fails with an error because fields cannot be deleted.
DThe entire document is deleted from Firestore.
Attempts:
2 left
💡 Hint

Think about what FieldValue.delete() does in Firestore updates.

❓ Configuration
intermediate
2:00remaining
Which Firestore update correctly deletes a nested field?

You want to delete the nested field address.zipcode from a Firestore document. Which update command will do this correctly?

AdocRef.update({ 'address.zipcode': firebase.firestore.FieldValue.delete() });
BdocRef.update({ address: { zipcode: firebase.firestore.FieldValue.delete() } });
CdocRef.update({ address: firebase.firestore.FieldValue.delete() });
DdocRef.update({ 'address.zipcode': null });
Attempts:
2 left
💡 Hint

Consider how Firestore handles dot notation for nested fields in updates.

❓ Architecture
advanced
2:00remaining
Impact of deleting fields on Firestore indexes

You delete a frequently queried field from many documents in a Firestore collection. What is the impact on Firestore indexes and query performance?

AFirestore requires manual index deletion for fields removed from documents.
BThe indexes keep the deleted field entries, causing queries to fail until manually rebuilt.
CDeleting fields does not affect indexes; queries on that field return empty results.
DThe indexes automatically update to remove the deleted field entries, improving query performance.
Attempts:
2 left
💡 Hint

Think about how Firestore maintains indexes when documents change.

❓ security
advanced
2:00remaining
Security rules effect on deleting fields in Firestore

You have a Firestore security rule that allows users to update their documents but denies deleting any fields. What happens if a user tries to delete a field using FieldValue.delete()?

AThe field is deleted despite the security rule because delete is a special operation.
BThe entire document update fails and the document is deleted.
CThe update is rejected by Firestore security rules; the field remains unchanged.
DThe field is set to null instead of being deleted.
Attempts:
2 left
💡 Hint

Consider how Firestore security rules control update operations.

✅ Best Practice
expert
3:00remaining
Best practice for deleting multiple fields atomically in Firestore

You need to delete multiple fields from a Firestore document in a single operation to keep data consistent. Which approach is best?

AUse a single <code>update()</code> call with all fields set to <code>FieldValue.delete()</code>.
BCall <code>update()</code> multiple times, each deleting one field sequentially.
CDelete the entire document and recreate it without the unwanted fields.
DRead the document, delete fields locally, then overwrite the entire document with <code>set()</code>.
Attempts:
2 left
💡 Hint

Think about atomicity and minimizing network calls.

Practice

(1/5)
1. What does the deleteField() function do in Firebase Firestore?
easy
A. It deletes the entire document.
B. It removes a specific field from a document.
C. It adds a new field to a document.
D. It updates the value of a field.

Solution

  1. Step 1: Understand the purpose of deleteField()

    This function is designed to remove a specific field from a Firestore document without deleting the whole document.
  2. Step 2: Differentiate from other operations

    Deleting a document removes all data, adding or updating changes or adds fields, but deleteField() only removes one field.
  3. Final Answer:

    It removes a specific field from a document. -> Option B
  4. Quick Check:

    deleteField() removes fields = A [OK]
Hint: Remember: deleteField() targets one field, not whole document [OK]
Common Mistakes:
  • Confusing field deletion with document deletion
  • Thinking deleteField() adds or updates fields
  • Assuming deleteField() works on collections
2. Which of the following is the correct syntax to delete a field named age from a Firestore document using updateDoc?
easy
A. updateDoc(docRef, deleteField('age'));
B. updateDoc(docRef, { deleteField: 'age' });
C. updateDoc(docRef, { age: null });
D. updateDoc(docRef, { age: deleteField() });

Solution

  1. Step 1: Recall the correct usage of deleteField()

    The function is used as a value in the update object for the field to delete, like { fieldName: deleteField() }.
  2. Step 2: Check each option's syntax

    updateDoc(docRef, { age: deleteField() }); correctly uses age: deleteField(). updateDoc(docRef, { deleteField: 'age' }); wrongly uses the function name as a key. updateDoc(docRef, { age: null }); sets the field to null, which does not delete it. updateDoc(docRef, deleteField('age')); incorrectly passes the field name as an argument to deleteField().
  3. Final Answer:

    updateDoc(docRef, { age: deleteField() }); -> Option D
  4. Quick Check:

    Use field: deleteField() inside updateDoc = A [OK]
Hint: Use field name as key and deleteField() as value [OK]
Common Mistakes:
  • Passing field name as argument to deleteField()
  • Setting field to null instead of deleting
  • Using deleteField as a key instead of a function
3. Given the following code snippet, what will be the content of the document after execution?
const docRef = doc(db, 'users', 'user1');
await updateDoc(docRef, { city: deleteField() });

Assuming the document initially has fields: { name: 'Anna', city: 'Paris', age: 30 }
medium
A. { name: 'Anna', city: 'Paris', age: 30 }
B. { name: 'Anna', city: null, age: 30 }
C. { name: 'Anna', age: 30 }
D. Document deleted

Solution

  1. Step 1: Understand what deleteField() does in updateDoc

    It removes the specified field from the document, so city will be removed.
  2. Step 2: Analyze the document after update

    Original fields are name, city, and age. After deletion, city is gone, leaving name and age.
  3. Final Answer:

    { name: 'Anna', age: 30 } -> Option C
  4. Quick Check:

    deleteField() removes field = { name, age } [OK]
Hint: Deleted fields disappear completely, not set to null [OK]
Common Mistakes:
  • Thinking deleted fields become null
  • Assuming document is deleted
  • Believing field value remains unchanged
4. You wrote this code to delete the field status from a Firestore document:
await updateDoc(docRef, { deleteField('status') });

Why does this code cause an error?
medium
A. Because deleteField must be used as a value, not as a key.
B. Because updateDoc cannot delete fields.
C. Because deleteField requires the document reference as argument.
D. Because status field does not exist.

Solution

  1. Step 1: Identify the syntax error in the update object

    The object passed to updateDoc must have keys as field names and values as the new values or deleteField(). Here, deleteField('status') is used as a key, which is invalid.
  2. Step 2: Correct usage of deleteField()

    The correct syntax is { status: deleteField() }, where status is the key and deleteField() is the value.
  3. Final Answer:

    Because deleteField must be used as a value, not as a key. -> Option A
  4. Quick Check:

    Field name as key, deleteField() as value = D [OK]
Hint: Use field name as key, deleteField() as value in updateDoc [OK]
Common Mistakes:
  • Using deleteField() as key instead of value
  • Passing arguments to deleteField()
  • Assuming updateDoc can't delete fields
5. You want to remove multiple fields phone and address from a Firestore document in a single update. Which code snippet correctly deletes both fields?
hard
A. await updateDoc(docRef, { phone: deleteField(), address: deleteField() });
B. await updateDoc(docRef, { deleteField('phone'), deleteField('address') });
C. await updateDoc(docRef, { phone: null, address: null });
D. await updateDoc(docRef, deleteField(['phone', 'address']));

Solution

  1. Step 1: Understand how to delete multiple fields

    Each field to delete must be a key in the update object with deleteField() as its value.
  2. Step 2: Evaluate each option

    await updateDoc(docRef, { phone: deleteField(), address: deleteField() }); correctly deletes both fields by specifying them separately. await updateDoc(docRef, { deleteField('phone'), deleteField('address') }); uses invalid syntax with multiple deleteField calls as keys. await updateDoc(docRef, { phone: null, address: null }); sets fields to null but does not delete them. await updateDoc(docRef, deleteField(['phone', 'address'])); incorrectly passes an array to deleteField, which is not supported.
  3. Final Answer:

    await updateDoc(docRef, { phone: deleteField(), address: deleteField() }); -> Option A
  4. Quick Check:

    Delete multiple fields by listing each with deleteField() = B [OK]
Hint: List each field with deleteField() in updateDoc object [OK]
Common Mistakes:
  • Trying to delete multiple fields with one deleteField call
  • Setting fields to null instead of deleting
  • Using deleteField as keys or passing arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard