📖 Scenario: You are managing a Firebase Firestore database for a small online store. Some product documents have an outdated field called discount that is no longer used. You want to clean up the database by deleting this discount field from all product documents.
🎯 Goal: Build a Firebase Firestore script that deletes the discount field from a single product document.
📋 What You'll Learn
Create a reference to a Firestore document with a specific product ID
Create an update object that deletes the discount field
Use the Firestore update method to remove the field
Ensure the code is valid and deployable in a Firebase environment
💡 Why This Matters
🌍 Real World
Cleaning up outdated fields in Firestore documents helps keep the database organized and reduces storage costs.
💼 Career
Knowing how to update and delete fields in Firestore is essential for Firebase developers managing real-time databases.
Progress0 / 4 steps
1
Create a Firestore document reference
Create a variable called productRef that references the Firestore document at collection products with document ID prod123. Use doc(db, "products", "prod123").
Firebase
Hint
Use the doc function with db, collection name, and document ID.
2
Create an update object to delete the discount field
Create a variable called updateData that deletes the discount field using deleteField() from firebase/firestore. Import deleteField and set updateData to { discount: deleteField() }.
Firebase
Hint
Import deleteField and use it to set the field value to be deleted.
3
Use updateDoc to delete the field
Call updateDoc with productRef and updateData to delete the discount field from the document.
Firebase
Hint
Use await updateDoc(productRef, updateData) to apply the update.
4
Add async function wrapper
Wrap the update code inside an async function called removeDiscountField and export it. The function should contain the await updateDoc(productRef, updateData) call.
Firebase
Hint
Define and export an async function that performs the update.
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
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.
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.
Final Answer:
It removes a specific field from a document. -> Option B
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
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() }.
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().
Final Answer:
updateDoc(docRef, { age: deleteField() }); -> Option D
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?
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
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.
Step 2: Correct usage of deleteField()
The correct syntax is { status: deleteField() }, where status is the key and deleteField() is the value.
Final Answer:
Because deleteField must be used as a value, not as a key. -> Option A
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
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.
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.