Per the new Firestore documentation, I see 2 new methods (arrayUnion, arrayRemove) for working with arrays nested within documents. These methods allow you to add and remove an array item, but how would I update an existing element?
相关问题
- adding sha1 in firebase app fails with error
- firebase storage cors strange Behaviour
- Firebase security rules difference between get() a
- Firebase security rules difference between get() a
- LoginActivty with Firebase & Facebook authenticati
相关文章
- How can make folder with Firebase Cloud Functions
- Firestore Update a document field Using Rest API
- How to convert a FCM token to APNS token?
- App not showing Notification receiving FCM when th
- Android Studio - Get Firebase token from GetIdToke
- How to combine Firestore orderBy desc with startAf
- Remove Duplicates from an Array of GeoFire Objects
- Is it possible to test a Firebase trigger locally?
If your array modification is not satisfied by arrayUnion or arrayRemove, you will have to read the document, modify the array values in memory, then update the array field with the new array in its entirety.
There is no update method because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.
Because we are creating apps that can be used in a multi user environment, try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 and in the same time some other user might want to add another item at index 0. For sure, you'll end up having very different results and why not, get even an ArrayIndexOutOfBoundsException. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index.
As Doug mentioned in his answer, if those two methods do not help you enough, you should get the entire document, get the array, modify it and add it back to the database.