0
0
Firebasecloud~5 mins

Array update operations (arrayUnion, arrayRemove) in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you store lists in a database, you often need to add or remove items without replacing the whole list. Firebase provides simple commands to add or remove items from arrays safely, even if many users update at the same time.
When you want to add a new tag to a list of tags on a document without overwriting existing tags.
When you need to remove a specific user ID from a list of participants in a chat room.
When multiple users can add their favorite items to a shared list without conflicts.
When you want to update a list field atomically to avoid race conditions.
When you want to keep a history of unique actions by adding only new entries to an array.
Commands
This command adds the item "apple" to the favorites array in the user123 document without removing existing items.
Terminal
firebase firestore:update --project example-project --collection users --doc user123 --data '{"favorites": FieldValue.arrayUnion("apple")}'
Expected OutputExpected
Updated document user123 in collection users.
--project - Specifies the Firebase project to use.
--collection - Specifies the Firestore collection.
--doc - Specifies the document ID to update.
This command removes the item "banana" from the favorites array in the user123 document if it exists.
Terminal
firebase firestore:update --project example-project --collection users --doc user123 --data '{"favorites": FieldValue.arrayRemove("banana")}'
Expected OutputExpected
Updated document user123 in collection users.
--project - Specifies the Firebase project to use.
--collection - Specifies the Firestore collection.
--doc - Specifies the document ID to update.
This command retrieves the current data of the user123 document to verify the array updates.
Terminal
firebase firestore:get --project example-project --collection users --doc user123
Expected OutputExpected
{ "favorites": ["apple", "orange"] }
--project - Specifies the Firebase project to use.
--collection - Specifies the Firestore collection.
--doc - Specifies the document ID to retrieve.
Key Concept

If you remember nothing else from this pattern, remember: use arrayUnion to add items safely without duplicates and arrayRemove to delete items from arrays atomically.

Common Mistakes
Trying to update arrays by replacing the whole array instead of using arrayUnion or arrayRemove.
This can overwrite other users' changes and cause data loss.
Use arrayUnion and arrayRemove to update arrays atomically without overwriting.
Using arrayUnion with items that are already in the array expecting duplicates.
arrayUnion prevents duplicates, so the item won't be added again.
Understand that arrayUnion only adds unique items; duplicates are ignored.
Using arrayRemove with an item not present in the array and expecting an error.
arrayRemove silently does nothing if the item is not found.
Know that arrayRemove is safe to use even if the item is missing.
Summary
Use arrayUnion to add new unique items to an array field without overwriting existing data.
Use arrayRemove to delete specific items from an array field safely.
Verify updates by retrieving the document after applying array operations.