Firestore安全规则-我可以查询具有特定字段的文档吗? [英] Firestore security rules - can I query for a document with specific fields?

查看:143
本文介绍了Firestore安全规则-我可以查询具有特定字段的文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以制定运行查询以检查是否找到匹配文档的安全规则吗?

Can you make security rules that runs a query to check if a matching document is found?

我正在构建一个系统,登录的用户可以对特定主题进行投票.

I'm building a system where a logged in user can vote on specific topics.

每个投票都将保存在其自己的文档中,并带有对用户,主题等的引用.

Every single vote will be saved in its own document, with a reference to the user, the topic etc.

我想制定一个安全规则,检查投票部分中是否已经存在具有该特定用户ID和主题ID的文档,并且只允许用户编写一个投票文档(不是这种情况).

I want to make a security rule that checks if there's already a document in the vote section with that specific user ID and topic ID present, and only let the user write a vote document it that's not the case.

我在文档中看不到任何查询选项,那么我有什么选择?

I can't see any query options in the documentation, so what are my options?

我可以以某种方式创建所有投票的索引,并在该索引中查找特定的文档路径吗?

Can I somehow create an index of all votes, and look for a specific document path in that index?

还是我应该根据用户ID和主题ID为投票提供一个自定义ID方案,以便可以找到它们?

Or should I give the votes a custom ID scheme, based on the user ID and the topic ID, so they can be found?

推荐答案

您不能对具有安全规则的条件执行查询.您所能做的就是使用已知路径在文档中get().也就是说,您必须知道其唯一标识的所有集合和文档ID.

You can't perform a query with conditions with security rules. All you can do is get() a document using its known path. That is to say, you must know all of its collections and document IDs that uniquely identify it.

听起来似乎可以创建其他文档(它们不必包含任何数据)是可行的,并且它们的存在可以表明您想要保护的条件.

It sounds like it might be feasible to create others documents (they don't have to contain any data), and their existence can signal the condition you want to protect.

(是的,有时数据建模必须考虑安全规则.)

(Yes, sometimes modeling of data has to take security rules into consideration.)

这篇关于Firestore安全规则-我可以查询具有特定字段的文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆