Goal/intention
The rule should grant access to a user document (which contains some personal information like email address) and also its subcollections for movie and tv watchlists, in case the user allows this for the requesting person by adding his/her email address to his shareWith field array.
However, with the request below using where I intent to get all user documents which grant access to a specific other user (i.e. sharing the watchlists). Currently, I solve this by having a separate collection called "conntections" which is readable for everybody and contains the uids of the users who want to share. And with that uid I currently can then access the user document itself with the watchlist. But it would be more effective if I could leave out this extra collection in case I could directly find out which user is sharing the watchlist with the current (logged in) user on the website.
As mentioned in the positive example of the Firestore Documentation I tried the query with the where condition, but it didn't work (premission error):
getDocs(query(collection(db, 'users'), where('shareWith', 'array-contains', email)))
.then(q => q.forEach(doc => console.log(doc.id, " => ", doc.data())))
;
Why does it not work with the following rule?
Hint: The get() is needed because I also want to access subcollections of the user, i.e. the watchlists, see Getting read access for subcollections of specific document in case a field matches in the main document only
match /users/{userId}/{documents=**} {
allow read: if request.auth.token.email in get(/databases/$(database)/documents/users/$(userId)).data.shareWith;
allow read, write: if request.auth.uid == userId;
}
I'm currently using the (working) query below as an in-between step to get the uid for accessing the document directly without the where condition:
return getDocs(query(collection(db, 'connections'), where('shareWith', 'array-contains', api.account.id)))
.then(qs => qs.docs.map(doc => doc.id))
;
For accessing the connections collection I can have a more open rule, because there is no personal data visible:
match /connections/{userId} {
allow read: if request.auth.uid != null;
allow read, write: if request.auth.uid == userId;
}
The information in connections is redundant, but currently necessary:

PS: What works with the rule above, is accessing a specific user document (and also subcollections) though (for example users/123), using the following request:
const
docRef = doc(db, 'users', uid)
;
return getDoc(docRef)
.then(docSnap => docSnap.data())
;


email". What does your rule intend to state?getcall in the rule here. Why can't you useresourceto look up theshareWithfield?