Is it possible to check which element of a document array is requested to be removed/added?
UPDATE 2020
You now actually can check for a removed item. All you have to do is subtract the new, reduced, array from the old one with removeAll()
and check the items of the array that it returns.
For example to allow only the removal one's own uid
from an array, you would do:
allow update: if
request.auth.uid != null
&& request.resource.data.users.size() == resource.data.users.size() - 1
&& resource.data.users.removeAll(request.resource.data.users)[0] == request.auth.uid
Specifically:
uid
) from the old one with removeAll()
and returns an array with their difference. In this case, it returns an array which contains only the single uid
that you chose to arrayRemove()
. Then we simply check that uid
- which can only exist at position [0]
- and make sure it is equal to the uid
of the authenticated user.As you probably noticed, queries in Cloud Firestore are very fast and this is because Firestore automatically creates an indexes for any fields you have in your document.
There are many posts out there that say that arrays don't work well on Cloud Firestore because when you have data that can be altered by multiple clients, it's very easy to get confused because you cannot know what is happening and on which particular field. If you're using a Map
and users want to edit several different fields, even the exact same field, we generally know what is happening. In an arrays, things are different. 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 another user wants to add another value at index 1, you'll end up having very different results and why not, 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. So use arrays only if don't care about the exact order that you store elements. Firestore added a few days ago some features to add or remove specific elements but only if don't care about the exact position of them. See here official documentation.
And to answer to your question:
Is it possible to check which element of a document array is requested to be removed/added?
No, you cannot!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With