英文:
Query Firestore collection on map
问题
我有一个包含成员和其他数据的“工作空间”数据库。对于每个工作空间,都有一个成员映射,其中键是用户ID,值是电子邮件。
它的结构如下:
Workspace01
--- name:
--- color:
--- members:
------ userid: "email@email.com"
我正在尝试查询当前用户是成员的工作空间。
在安全规则中,我可以尝试像这样做:
allow read if uid in resource.members.keys()
它可以正常工作(我认为)。它允许读取等操作。
然而,如果我尝试查询相同的内容,例如在kotlin/android中,它会给出权限错误:
collection.whereArrayContains("members.keys", uid)
我还尝试过使用FieldPath.of()
,或者whereNotEqual("members[uid]", null)
,仍然是同样的情况。
我还尝试过将电子邮件作为键,uid作为值... 显然没有区别。
还有一个建议是使用orderBy("members.uid")
。这也会产生“不良权限”错误。然而,在Firebase控制台中,这个orderBy查询却运行得很好。
我已经完全没有主意了。
帮助?
英文:
I have database of Workspaces
with members and some other data. For each workspace there is a members Map, where key is the UserId and value is the email
It is structured like this:
\Workspace01
--- name:
--- color:
--- members:
------ userid: "email@email.com"
I am trying to query workspaces where the current user is a members.
In Security Rules for instance, I can try something like
allow read if uid in resource.members.keys()
and it works fine (I think). It allows read, etc.
However, if I try to query the same thing, for instance in kotlin/android, it gives a permission error:
collection.whereArrayContains("members.keys", uid )
I have also tried FieldPath.of()
, or whereNotEqual("members[uid]", null)
, still the same thing.
I also tried using emails as keys and uid as values... Obviously no difference
There also was a suggestion of using orderBy("memebers.uid")
. That also gives a "bad permission". However, this orderBy query works just fine in the Firebase console
I am completely out of ideas.
Help?
答案1
得分: 0
你说members
字段是一个map
,所以不能使用whereArrayContains
来查询它是有道理的。
如果你想检查uid
子字段是否具有特定值,可以这样做:
collection.whereEqualTo("members.theUidValue", "theEmailValue")
我不太确定你的安全规则是否允许这样操作,所以最好从更宽松的规则开始,先看看查询是否有效。
如果你只想检查UID是否存在,这在map结构上是不可能的。要实现这一点,可以添加一个附加的数组字段,只包含来自map的UID值(例如memberUIDs
),然后在这个数组上使用whereArrayContains
进行查询。
英文:
You say that the members
field is a map
, so it makes sense that you can't query it with whereArrayContains
.
If you want to check if the uid
subfield has a specific value, that'd be:
collection.whereEqualTo("members.theUidValue", "theEmailValue")
I'm not entirely sure that your security rules will allow this, so I'd start with more relaxed rules to first see if the query works.
If you want to simply check whether the UID exists, that isn't possible on a map structure. To allow that, add an additional array field with just the UID values from the map (e.g. memberUIDs
), and then query with whereArrayContains
on that.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论