Android Firestore规则-查询文档字段而不公开整个集合

Android Firestore规则-查询文档字段而不公开整个集合,android,google-cloud-firestore,firebase-security,Android,Google Cloud Firestore,Firebase Security,我有一个users集合,其中包含包含各种字段的文档。每个文档ID都是firebase用户ID,字段保存用户数据 我使用以下规则: match /databases/{database}/documents { // Make sure the uid of the requesting user matches name of the user document. match /users/{userId} { allow read, update, delete: if req

我有一个
users
集合,其中包含包含各种字段的文档。每个文档ID都是firebase用户ID,字段保存用户数据

我使用以下规则:

match /databases/{database}/documents {
  // Make sure the uid of the requesting user matches name of the user document.
  match /users/{userId} {
    allow read, update, delete: if request.auth.uid == userId;
    allow create: if request.auth.uid != null;
  }
}
用户可以选择保存在数组字段中的“喜爱的歌曲”。我现在需要查询数据库,查找那些在收藏夹中添加了特定歌曲的用户。我创建了以下查询:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference usersRef = db.collection("users");
usersRef.whereArrayContains("favouriteSongs", "id_of_song");
运行查询时,返回以下错误:

com.google.firebase.firestore.FirebaseFirestoreException:权限\被拒绝:权限缺失或不足

我显然需要修改Firestore规则,以允许用户查询“FavoriteShongs”字段;但是,我不想向所有人公开所有用户数据

是否有办法公开“FavoriteShongs”数据(仅包含ID)并确保用户集合的其余部分被限制读取


谢谢

遗憾的是,不,安全规则是在文档/查询级别。因此,如果安全规则允许访问,那么用户将有权访问整个文档

如果
FavoriteShongs
是唯一可以/应该公开读取的字段,则该字段应位于不同的文档中

有几种选择。您可以有一个名为
favoriteshongs
的根级集合,其中的用户文档仅包含公共数据。这将使您能够灵活地轻松查找用户的收藏夹,或查询所有数据以查找有多少用户喜欢某首歌曲


我建议只有用户自己才能阅读自己的文档,涉及隐私等(如果相关,可能是GDPR)。

如果您只想向用户公开文档的一部分,那么DarkNeuron的回答是正确的。我只是想知道你是否真的不喜欢。