Deleting fields in Firebase - Time & Space Complexity
When deleting fields in Firebase documents, it's important to know how the time to complete the operation changes as you delete more fields.
We want to understand how the number of fields affects the work Firebase does behind the scenes.
Analyze the time complexity of the following operation sequence.
const docRef = firestore.collection('users').doc('user123');
await docRef.update({
field1: firebase.firestore.FieldValue.delete(),
field2: firebase.firestore.FieldValue.delete(),
field3: firebase.firestore.FieldValue.delete(),
});
This code deletes three fields from a single document in Firestore.
- Primary operation: One update API call to Firestore that deletes multiple fields.
- How many times: The update call happens once regardless of number of fields, but each field deletion is included in the update.
As you delete more fields in a single update, the work grows roughly in direct proportion to the number of fields.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 update call deleting 10 fields |
| 100 | 1 update call deleting 100 fields |
| 1000 | 1 update call deleting 1000 fields |
Pattern observation: The number of API calls stays the same, but the amount of data processed grows linearly with the number of fields deleted.
Time Complexity: O(n)
This means the time to delete fields grows linearly with how many fields you delete in one update.
[X] Wrong: "Deleting multiple fields in one update is just as fast as deleting one field because it's a single API call."
[OK] Correct: Even though there is one API call, the server processes each field deletion, so more fields mean more work and longer time.
Understanding how batch updates scale helps you design efficient data operations and shows you can think about performance beyond just counting API calls.
"What if we delete fields one by one with separate update calls? How would the time complexity change?"
Practice
deleteField() function do in Firebase Firestore?Solution
Step 1: Understand the purpose of
This function is designed to remove a specific field from a Firestore document without deleting the whole document.deleteField()Step 2: Differentiate from other operations
Deleting a document removes all data, adding or updating changes or adds fields, butdeleteField()only removes one field.Final Answer:
It removes a specific field from a document. -> Option BQuick Check:
deleteField()removes fields = A [OK]
- Confusing field deletion with document deletion
- Thinking deleteField() adds or updates fields
- Assuming deleteField() works on collections
age from a Firestore document using updateDoc?Solution
Step 1: Recall the correct usage of
The function is used as a value in the update object for the field to delete, likedeleteField(){ fieldName: deleteField() }.Step 2: Check each option's syntax
updateDoc(docRef, { age: deleteField() }); correctly usesage: 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 todeleteField().Final Answer:
updateDoc(docRef, { age: deleteField() }); -> Option DQuick Check:
Use field: deleteField() inside updateDoc = A [OK]
- Passing field name as argument to deleteField()
- Setting field to null instead of deleting
- Using deleteField as a key instead of a function
const docRef = doc(db, 'users', 'user1');
await updateDoc(docRef, { city: deleteField() });Assuming the document initially has fields:
{ name: 'Anna', city: 'Paris', age: 30 }Solution
Step 1: Understand what
It removes the specified field from the document, sodeleteField()does in updateDoccitywill be removed.Step 2: Analyze the document after update
Original fields arename,city, andage. After deletion,cityis gone, leavingnameandage.Final Answer:
{ name: 'Anna', age: 30 } -> Option CQuick Check:
deleteField() removes field = { name, age } [OK]
- Thinking deleted fields become null
- Assuming document is deleted
- Believing field value remains unchanged
status from a Firestore document:await updateDoc(docRef, { deleteField('status') });Why does this code cause an error?
Solution
Step 1: Identify the syntax error in the update object
The object passed toupdateDocmust have keys as field names and values as the new values ordeleteField(). Here,deleteField('status')is used as a key, which is invalid.Step 2: Correct usage of
The correct syntax isdeleteField(){ status: deleteField() }, wherestatusis the key anddeleteField()is the value.Final Answer:
BecausedeleteFieldmust be used as a value, not as a key. -> Option AQuick Check:
Field name as key, deleteField() as value = D [OK]
- Using deleteField() as key instead of value
- Passing arguments to deleteField()
- Assuming updateDoc can't delete fields
phone and address from a Firestore document in a single update. Which code snippet correctly deletes both fields?Solution
Step 1: Understand how to delete multiple fields
Each field to delete must be a key in the update object withdeleteField()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.Final Answer:
await updateDoc(docRef, { phone: deleteField(), address: deleteField() }); -> Option AQuick Check:
Delete multiple fields by listing each with deleteField() = B [OK]
- Trying to delete multiple fields with one deleteField call
- Setting fields to null instead of deleting
- Using deleteField as keys or passing arrays
Ready to prove it?
Pass the test and this pattern moves unknown
