What is the way to remove an item in an index from array of map in firestore?
I have an array of maps as in the picture I attached, I need to delete an index of array 'cart'.
What is the way to remove an item in an index from array of map in firestore?
I have an array of maps as in the picture I attached, I need to delete an index of array 'cart'.
You cannot delete an array item using its index. This is intentional because, there may be multiple clients adding/removing the items in the array, which will change the indexes. Hence, you must delete the items by values. More details here.
Furthermore, if you need conditional delete, like "delete all items with count 1", then you need to handle this case yourself, on the client side as: get the array, update it, push it back.
Depending on your requirements, if you can change your data structure to a map instead of an array then you can delete at a specific key.
cart: {
0: {count: 3...},
1: {count: 1...}
}
Then on your client (details here):
docRef.update.{['chat.' + index]: firebase.firestore.FieldValue.delete()};