Is it possible to check which element of a document array is requested to be removed/added?
2 Answers
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:
- The first rule checks for user authentication
- The second rule makes sure that the new array is exactly 1 item shorter than the old one (so that only 1 item can be removed at a time)
- The third rule subtracts the new array (now reduced by 1
uid
) from the old one withremoveAll()
and returns an array with their difference. In this case, it returns an array which contains only the singleuid
that you chose toarrayRemove()
. Then we simply check thatuid
- which can only exist at position[0]
- and make sure it is equal to theuid
of the authenticated user.

- 415
- 5
- 11
-
1Where do we find documentation for operations like this? It would be nice to have a language reference for firestore rules but all I can really find is a vague guide written in prose..! – Ed_ Sep 03 '20 at 08:30
-
@EdHinchliffe I feel you and unfortunately I don't know of any resource better than stackoverflow. It's my goto place whenever official documentation fails me, hoping that some user has had the same problem and managed to solve it and write it in a concise way. This is how I stumbled on this post on the first place, searching for the answer. – Konstantinos T. Sep 04 '20 at 09:13
-
https://firebase.google.com/docs/reference/rules/rules.List – Jonathan Sep 19 '21 at 01:19
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!

- 130,605
- 17
- 163
- 193
-
Is there everything alright, can I help you with other informations? – Alex Mamo Oct 30 '18 at 03:12
-
So I'm asking the question since I'm thinking about a structure of my firebase app where users are able to add friends. Since the only thing the client needs is to get the friends uid I'm currently using one "friend" document per user where the uids are stored within an array. However, this is not the best solution concerning the rules system of the Firestore. On the other hand I don't want to have one document per friend since the data needs to be fresh every time and I'm saving reads when another user wants to see all friends of a specific user. Is there any better way to do this? – ktm125 Oct 30 '18 at 18:54
-
There is no better or correct way. Regarding your last question, please see my answer from this **[post](https://stackoverflow.com/questions/53053768/what-is-the-correct-way-to-structure-this-kind-of-data-in-firestore/53057707#53057707)**, for a better understanding. – Alex Mamo Oct 31 '18 at 07:02