查询 Firestore 集合中的地图

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月18日 19:39:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定