简体   繁体   English

Firebase 防火墙查询

[英]Firebase firestore query

I have 2 fields with other fields named ' uId ' and ' sellerUId ' in my collections as in the screenshot below.我的 collections 中有 2 个字段,其他字段名为“ uId ”和“ sellerUId ”,如下面的屏幕截图所示。 Both are Auth IDs.两者都是身份验证 ID。 I want to filter uId == auth_id or sellerUId == auth_id .我想过滤uId == auth_idSellerUId == auth_id

在此处输入图像描述

I have tried the following query:我尝试了以下查询:

.doc('doc name')
  .collection('collection name')
  .where(['uId', 'sellerUId'], isEqualTo: 'auth_id')
  .get()
  .then((QuerySnapshot querySnapshot) {
    for (var doc in querySnapshot.docs) {
      print(doc.id);
    }
  });

Firestore does not support OR queries on multiple fields. Firestore 不支持对多个字段进行 OR 查询。 You would need 2 different queries - one for uid == auth_id and another for sellerUid == auth_id .您将需要 2 个不同的查询 - 一个用于uid == auth_id ,另一个用于sellerUid == auth_id

For this use case, you can store a single array in the document users that contains both userId and sellectUid like this so you get fetch these documents in a single query:对于这个用例,您可以像这样在包含userIdsellectUid的文档users中存储一个数组,这样您就可以在单个查询中获取这些文档:

{
  users: ["uid", "sellerUid"],
  ...otherFields
}

Then you can use array-contains operator:然后您可以使用array-contains运算符:

.doc('doc name')
  .collection('collection name')
  .where("users", arrayContains: "auth_id");
  .get()
  .then((QuerySnapshot querySnapshot) {
    for (var doc in querySnapshot.docs) {
      print(doc.id);
    }
  });

This will fetch all documents where the users array contains auth_id .这将获取users数组包含auth_id的所有文档。 You can secure this with the following security rules (assuming auth_id is UID of current user):您可以使用以下安全规则来保护它(假设 auth_id 是当前用户的 UID):

match /collection/{docId} {
  allow read: if request.auth.uid in resource.data.users;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM