0
0
Firebasecloud~20 mins

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

Choose your learning style9 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.