Firestore规则,什么是集合和文档?
问题描述:
我的意思是,我知道这些是什么,但是我有点迷惑有关安全规则,例如:Firestore规则,什么是集合和文档?
service cloud.firestore {
match /databases/{database}/documents {
// This is probably a mistake
match /spaceships { // <= what is this a collection or a document?
allow read;
// In spite of the above line, a user can't read any document within the
// spaceship collection.
}
}
}
火力地堡文件说:
规则集合并不适用于内的文档那个集合。在集合级别而不是文档级别写入安全规则是不寻常的(也可能是错误)。
这意味着这match /spaceships {...
是一个收集权?
但后来,我们有这样的:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**}{ // <= what is this a document or a collection?
allow read, write: if true;
}
}
}
我不明白的是这match /{document=**}{...
文档?还是集合?我的意思是在收集水平。
答
路径在公司的FireStore是交替的收藏和文件:/collection/document/subcollection/subdocument
例如:
// Matches kaylee, the mechanic on serenity
/spaceships/serenity/crew/kaylee/...
当使用安全规则,你可以指定通配符:
// This will match any spaceship, and any crewmember
/spaceships/{spaceshipId}/crew/{crewmemberId}/...
现在,假设你有spaceships
下的另一个子集合:
/spaceships/{spaceshipId}/stowaways/{stowawayId}/...
如果要编写针对多个子集的规则,你需要:
// You could use multiple single wildcards
/spaceships/{spaceshipId}/{crewOrStowaway}/{crewOrStowawayId}/...
// Or you could use a multi-segment wildcard
/spaceships/{spaceshipId}/{allShipInformation=**}
这将返回allShipInformation
作为路径,这将在和路径下的匹配所有文件和集合。请注意,它是一个或多个路径段,而不是零个或更多。
你可以阅读更多关于这个in the docs
答
在你的第一个例子是/spaceships
在集合级别。正如您引用的报价中所述,在这里放置一条规则是没有用的,因为它不会应用于集合中的任何文档。
在第二个示例中/{document=**}
处于集合级别,但使用的是recursive wildcard。简而言之,这是将规则应用于此集合中的文档以及此集合的任何子集合中的任何文档。
这允许你写:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**}{
allow read, write: if true;
}
}
}
相反的:
service cloud.firestore {
match /databases/{database}/documents {
match /spaceships/{shipId} {
allow read, write: if true;
}
match /spaceships/{shipId}/crew/{crewMemberId} {
allow read, write: if true;
}
}
}