使用where(in :)和where(isEqual :)的Firestore查询

使用where(in :)和where(isEqual :)的Firestore查询

问题描述:

在Firebase Firestore中是否可以进行以下组合 where(in:) where(isEqualTo:)查询?

Is the following composite where(in:) and where(isEqualTo:) query possible in Firebase Firestore?

(快速)

let taskDocIds = ["0ArJnMgTfDkvolUcZR8E", "0cdcZsfaEJQpsiByJIgH"]

return db.collection("tasks")
      .whereField("status", isEqualTo: "approved")
      .whereField(Firebase.FieldPath.documentID(), in: taskDocIds)
      .limit(to: 100)
      .getDocuments(as: TaskModel.self)

我的安全规则强制执行,只有具有 status (经 approved 批准)的任务才能被读取.但是,当ID在 taskDocIds 数组中的任何任务的 status 具有 status 状态时,上面的查询将导致错误(请参阅下文).除 approved 之外的code>.

My security rules enforce that only tasks with status as approved may be read. However the the query above results in an error (see below) when the status of any of the tasks whose id is in the taskDocIds array has a status other than approved.

错误:Domain = FIRFirestoreErrorDomain代码= 7缺少权限".

Error: Domain=FIRFirestoreErrorDomain Code=7 "Missing or insufficient permissions."

如果可以查询,是否需要创建一个复合索引?如果是这样,如何去手动配置(使用Firebase控制台)一个FieldPath文档ID?

If the query is possible, does a composite index need to be created? And if so, how does one go about configuring that manually (using Firebase Console) for a FieldPath document id?

添加索引永远无法解决权限错误.

Permission errors can never be resolved by adding an index.

问题是该规则不知道是否允许整个 taskDocIds 的整个集合,而无需单独检查它们. Firebase安全规则不是过滤器,因此,在执行查询时,规则不会单独阅读和检查每个文档,而只会为您提供匹配的文档.如果不能保证每个可以匹配查询的文档都完全 匹配查询,那么它只会使整个规则失效.

The problem is that the rule doesn't know if your entire set of taskDocIds would be allowed without checking them all individually. Firebase security rules are not filters, so, when performing a query, the rule will not read and check each document individually and give you only the ones that match. Without assurance that every document that could match the query absolutely does match the query, it simply fails the entire rule.

您的替代方法是简单地分别每个单独地 getDocument().对于单个文档,规则检查该文档的各个字段.

Your alternative is to simply getDocument() each one individually. With individual document gets, the rule will check the individual fields of the document.