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

查看:19
本文介绍了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天全站免登陆