Firestore 规则,什么是集合和文档?

Ahs*_*ath 4 firebase-security google-cloud-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.
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Firebase 文档说:

集合规则不适用于该集合中的文档。在集合级别而不是文档级别编写安全规则是不寻常的(并且可能是错误的)。

这意味着这match /spaceships {...是一个集合对吗?

但后来我们有这个:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**}{ // <= what is this a document or a collection?
      allow read, write: if true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我不明白这match /{document=**}{...是一个文件吗?还是一个集合?我的意思是在收藏层面。

Mik*_*ald 5

Firestore 中的路径是交替的集合和文档: /collection/document/subcollection/subdocument

例如:

// Matches kaylee, the mechanic on serenity
/spaceships/serenity/crew/kaylee/...
Run Code Online (Sandbox Code Playgroud)

使用安全规则时,您可以指定通配符:

// This will match any spaceship, and any crewmember
/spaceships/{spaceshipId}/crew/{crewmemberId}/...
Run Code Online (Sandbox Code Playgroud)

现在假设您在 下有另一个子集合spaceships

/spaceships/{spaceshipId}/stowaways/{stowawayId}/...
Run Code Online (Sandbox Code Playgroud)

如果要针对多个子集合编写规则,则需要:

// You could use multiple single wildcards
/spaceships/{spaceshipId}/{crewOrStowaway}/{crewOrStowawayId}/...

// Or you could use a multi-segment wildcard
/spaceships/{spaceshipId}/{allShipInformation=**}
Run Code Online (Sandbox Code Playgroud)

这将allShipInformation作为路径返回,它将匹配该路径及其下方的所有文档集合。请注意,它是一个或多个路径段,而不是零个或多个。

您可以在文档中阅读有关此内容的更多信息