我的 Firestore 数据库结构如下:
/collection1/<docid1>/collection2/<docid2>/field1 (authenticated user)
/collection3/<docid2>/field2 (restricted access)
我只想检索 collection1 下的 collection2 中具有特定 field1 值的所有文档。
我可以通过使用“集合组”并过滤“文档 Id”和 field1 从查询生成器中执行查询,但是当我通过
:runQuery
从 REST API 尝试等效操作时
{
"structuredQuery": {
"from": [{
"allDescendants": true,
"collectionId": "collection2"
}],
"orderBy": [{
"direction": "ASCENDING",
"field": {
"fieldPath": "field3"
}
}],
"where": {
"compositeFilter": {
"filters": [{
"fieldFilter": {
"field": {
"fieldPath": "field1"
},
"op": "EQUAL",
"value": {
"stringValue": "<something>"
}
}
}, {
"fieldFilter": {
"field": {
"fieldPath": "uid"
},
"op": "LESS_THAN",
"value": {
"stringValue": "/collection1/z"
}
}
}, {
"fieldFilter": {
"field": {
"fieldPath": "uid"
},
"op": "GREATER_THAN",
"value": {
"stringValue": "/collection1/0"
}
}
}],
"op": "AND"
}
}
}
}
我不断得到
"error": {
"code": 403,
"message": "Missing or insufficient permissions.",
"status": "PERMISSION_DENIED"
}
规则如下(为了简单起见,我只提到了读取权限)
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /collection1/{docid1} {
allow read: if request.auth != null;
match /collection2/{docid2} {
allow read: if request.auth != null;
}
}
match /collection3/{docid2} {
allow read: if request.auth != null && request.auth.uid == docid2;
}
}
}
uid
相当于集合查询生成器中的Document ID
吗?如果没有,有没有办法通过 Document ID
作为查询构建器进行过滤?
我怀疑 REST API 正在深入搜索 (allDescendants = true),因此它尝试在
/collection3
中查找 collection2
。有没有办法告诉查询将搜索仅限于路径/collection1
?
查询生成器中文档 ID 字段的等效项是
"__NAME__"
。
要允许集合组查询,您需要带有
match
子句的规则,如基于集合组的安全和查询文档中所示的规则:
match /{path=**}/[COLLECTION_ID]/{doc}
path=**
部分确保它与集合组查询匹配,因为此规则与您为[COLLECTION_ID]
输入的任何名称匹配,无论它存在于数据库中。
要限制到某个路径,您可以执行路径前缀过滤器,如下所示:CollectionGroupQuery,但将搜索限制为特定文档下的子集合