java如何编写Firebase Firestore多对多关系的读取规则
我在Firebase Firestore中具有以下数据结构,以表示客户端和用户之间的多对多关系:
Clients
clientId1 {
users (object): {
userId1: true
userId2: true
}
}
clientId2 {
users (object): {
userId1: true
}
}
我使用以下查询在Android上进行查询:
db.collection("clients").whereEqualTo("users."+uid, true);
对于userId2,查询应该只返回client1
如果我将规则设置为(允许读取:如果为true;)当我执行上面的查询时,返回了正确的客户机
我还想设置一个数据库规则,以防止userId2看到client2
我尝试了此规则,但未返回任何结果:
match /clients/{clientId} {
//Allow read if the user exists in the user collection for this client
allow read: if users[request.auth.uid] == true;
}
我还尝试:
match /clients/{clientId} {
//Allow read if the user exists in the user collection for this client
allow read: if resource.data.users[request.auth.uid] == true;
}
但上述规则都不会返回任何客户端
我该如何编写规则
# 1 楼答案
根据关于Firestore Security Rules的正式文件:
因此,您不能使用安全规则过滤数据库中存在的文档
# 2 楼答案
我将回答我自己的问题,因为我只是在做一些愚蠢的事情
我的数据结构很好,规则的正确语法如下:
鉴于此:
此Android查询确实正确实现了规则的正确筛选器:
我还没有正确实现适配器。我想看看是否可以先让正确的数据结构/规则/查询正常工作。我是从另一个侦听器调用它的,该侦听器侦听整个客户端集合(该规则失败),因此没有调用此查询。前面我将规则设置为(允许读取:如果为true;)最初的侦听器正在执行我的查询并返回正确的结果。这让我相信我的规则是错误的,而事实并非如此