Bird
Raised Fist0
Firebasecloud~20 mins

Array update operations (arrayUnion, arrayRemove) in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
1:30remaining
What is the final array after using arrayUnion?
You have a Firestore document with a field tags containing ["cloud", "storage"]. You run an update with arrayUnion("storage", "database"). What will be the new value of tags?
A["cloud", "database"]
B["database"]
C["cloud", "storage", "database"]
D["cloud", "storage", "storage", "database"]
Attempts:
2 left
💡 Hint
arrayUnion adds elements only if they are not already present.
❓ service_behavior
intermediate
1:30remaining
What happens when arrayRemove removes a non-existing element?
A Firestore document has a field features with value ["auth", "hosting"]. You run an update with arrayRemove("functions"). What will be the value of features after the update?
A["auth", "hosting"]
B[]
C["functions"]
D["auth"]
Attempts:
2 left
💡 Hint
arrayRemove only removes elements if they exist.
❓ Configuration
advanced
2:00remaining
Which update operation correctly adds multiple unique elements to an array field?
You want to update a Firestore document's permissions array to add "read" and "write" without duplicates. Which update code snippet is correct?
Aupdate({ permissions: arrayRemove("read", "write") })
Bupdate({ permissions: arrayUnion(["read", "write"]) })
Cupdate({ permissions: arrayRemove(["read", "write"]) })
Dupdate({ permissions: arrayUnion("read", "write") })
Attempts:
2 left
💡 Hint
arrayUnion accepts multiple arguments, not an array argument.
❓ security
advanced
2:00remaining
What is a security risk when using arrayUnion with user input?
If you use arrayUnion to add user-provided tags to a Firestore array field without validation, what risk might occur?
AarrayUnion will cause a syntax error if tags are invalid.
BUsers can add malicious or inappropriate tags that persist in the database.
CUsers cannot add duplicate tags due to arrayUnion restrictions.
DarrayUnion will delete existing tags accidentally.
Attempts:
2 left
💡 Hint
Think about what happens if you trust user input blindly.
❓ Architecture
expert
2:30remaining
How to efficiently maintain a unique list of active users in Firestore using array operations?
You want to keep a Firestore document field activeUsers updated with unique user IDs as users become active or inactive. Which approach best ensures uniqueness and efficient updates?
AUse arrayUnion to add user IDs when active and arrayRemove to remove when inactive.
BReplace the entire activeUsers array with a new array on every update.
CUse arrayRemove to add user IDs and arrayUnion to remove them.
DStore activeUsers as a map with user IDs as keys instead of an array.
Attempts:
2 left
💡 Hint
Consider how arrayUnion and arrayRemove work for adding/removing unique elements.

Practice

(1/5)
1. What does the arrayUnion operation do in Firebase Firestore?
easy
A. Removes all elements from an array field
B. Adds unique elements to an array field without duplicates
C. Replaces the entire array with a new one
D. Sorts the array elements in ascending order

Solution

  1. Step 1: Understand arrayUnion purpose

    The arrayUnion operation adds elements to an array field only if they are not already present, ensuring no duplicates.
  2. Step 2: Compare with other options

    The other options describe removing all elements, replacing the array, or sorting, which are not what arrayUnion does.
  3. Final Answer:

    Adds unique elements to an array field without duplicates -> Option B
  4. Quick Check:

    arrayUnion adds unique items without duplicates [OK]
Hint: arrayUnion only adds new unique items to arrays [OK]
Common Mistakes:
  • Thinking arrayUnion removes items
  • Confusing arrayUnion with replacing arrays
  • Assuming arrayUnion sorts the array
2. Which of the following is the correct syntax to add an element 'blue' to an array field 'colors' using arrayUnion in Firestore?
easy
A. docRef.update({ colors: arrayUnion('blue') })
B. docRef.update({ colors: arrayRemove('blue') })
C. docRef.set({ colors: arrayUnion('blue') })
D. docRef.add({ colors: arrayUnion('blue') })

Solution

  1. Step 1: Identify correct method for updating fields

    To update an existing document's field, update() is used, not set() or add().
  2. Step 2: Use arrayUnion to add element

    The syntax arrayUnion('blue') correctly adds 'blue' to the array without duplicates.
  3. Final Answer:

    docRef.update({ colors: arrayUnion('blue') }) -> Option A
  4. Quick Check:

    Use update() with arrayUnion() to add items [OK]
Hint: Use update() with arrayUnion() to add array items [OK]
Common Mistakes:
  • Using arrayRemove instead of arrayUnion
  • Using set() which overwrites the whole document
  • Using add() which is for collections, not fields
3. Given the Firestore document field tags: ['news', 'tech'], what will be the value of tags after this update?
docRef.update({ tags: arrayUnion('tech', 'science') })
medium
A. ['news', 'tech', 'science']
B. ['news', 'science']
C. ['tech', 'science']
D. ['news', 'tech', 'tech', 'science']

Solution

  1. Step 1: Understand arrayUnion behavior with existing elements

    arrayUnion adds only new unique elements. 'tech' already exists, so it won't be duplicated.
  2. Step 2: Add new element 'science'

    'science' is new, so it will be added to the array.
  3. Final Answer:

    ['news', 'tech', 'science'] -> Option A
  4. Quick Check:

    arrayUnion adds only new unique items [OK]
Hint: arrayUnion skips duplicates, adds only new items [OK]
Common Mistakes:
  • Expecting duplicates to be added
  • Thinking arrayUnion removes items
  • Confusing arrayUnion with arrayRemove
4. You want to remove the string 'urgent' from the array field 'labels' in a Firestore document. Which code snippet contains an error?
medium
A. docRef.update({ labels: arrayRemove('urgent', 'important') })
B. docRef.update({ labels: arrayRemove(['urgent']) })
C. docRef.update({ labels: arrayRemove() })
D. docRef.update({ labels: arrayRemove('urgent') })

Solution

  1. Step 1: Check valid usage of arrayRemove

    arrayRemove requires at least one argument specifying which elements to remove.
  2. Step 2: Analyze each option

    docRef.update({ labels: arrayRemove() }) calls arrayRemove with no arguments, which is invalid and causes an error.
    docRef.update({ labels: arrayRemove(['urgent']) }) attempts to remove the array ['urgent'] (won't match/remove string 'urgent').
    docRef.update({ labels: arrayRemove('urgent', 'important') }) removes both if present.
    docRef.update({ labels: arrayRemove('urgent') }) correctly removes 'urgent'.
  3. Final Answer:

    docRef.update({ labels: arrayRemove() }) -> Option C
  4. Quick Check:

    arrayRemove needs at least one element argument [OK]
Hint: arrayRemove must have at least one element argument [OK]
Common Mistakes:
  • Calling arrayRemove with no arguments
  • Passing an array inside arrayRemove instead of elements
  • Confusing arrayRemove with arrayUnion
5. You have a Firestore document with an array field participants: ['alice', 'bob', 'carol']. You want to update it so that 'bob' is removed and 'dave' is added, ensuring no duplicates. Which update code achieves this?
hard
A. docRef.update({ participants: arrayRemove('bob'), participants: arrayUnion('dave') })
B. docRef.update({ participants: arrayRemove('bob', 'dave') })
C. docRef.update({ participants: arrayUnion('dave') })
D. docRef.update({ participants: arrayRemove('bob') }); docRef.update({ participants: arrayUnion('dave') })

Solution

  1. Step 1: Understand Firestore update limitations

    You cannot apply both arrayRemove and arrayUnion to the same field in a single update object (duplicate keys overwrite, only last applies).
  2. Step 2: Analyze options

    docRef.update({ participants: arrayRemove('bob'), participants: arrayUnion('dave') }): duplicate keys, only arrayUnion('dave') applies (adds 'dave', keeps 'bob').
    docRef.update({ participants: arrayRemove('bob', 'dave') }): removes 'bob' ('dave' ignored since absent), no add.
    docRef.update({ participants: arrayUnion('dave') }): adds 'dave', no remove.
    docRef.update({ participants: arrayRemove('bob') }); docRef.update({ participants: arrayUnion('dave') }): first removes 'bob', then adds 'dave' -> ['alice', 'carol', 'dave'].
  3. Final Answer:

    docRef.update({ participants: arrayRemove('bob') }); docRef.update({ participants: arrayUnion('dave') }) -> Option D
  4. Quick Check:

    Repeated keys overwrite; combine operations carefully [OK]
Hint: Firestore update keys must be unique; combine operations carefully [OK]
Common Mistakes:
  • Using duplicate keys in update object
  • Expecting multiple updates in one call with repeated keys
  • Not using transactions for complex updates
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