I have this data in a Firestore document:
{
name: "Tom",
groups: [
0: {
groupId: "12345",
moreInfo: "bla"
},
1: {
groupId: "67890",
moreInfo: "bla"
},
}
I want to limit access using firestore rules.
When a groupId exists in the groups array.
I think it should look something like this:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{docId} {
allow read, write: if "12345" in resource.data.groups.???;
}
}
}
Is it possible to query a value from an object in an array without giving a specific index in the array?
I have not managed to find a function that will allow this yet.
Alternatively I might have to create a sub collection or another document that stores this relationship in a normalised manner. (Although I am not sure what the best approach would be)
1
Is it possible to query a value from an object in an array without giving a specific index in the array?
No, not possible with security rules.
In fact, security rules don’t have the ability to iterate (or functions like map) over lists or maps at all. You have to know specifically what you’re looking for.
If you have specific requirements for complex items in lists, then you might be better off moving the items in that list into documents in a subcollection. Either that, or control access to the data using a secure backend code only (and not allowing client apps to modify that data at all).
1