【问题标题】:Firebase Firestore OR query (Android Studio)Firebase Firestore OR 查询 (Android Studio)
【发布时间】:2020-06-08 23:33:23
【问题描述】:

近 2 天来,我一直在尝试查找查询 我想从文档 4 个字段(customer1、customer2、customer3、customer4)中搜索 id(当前用户 id) 这是firestore文档图片

试过这个查询

final Query userQuery = collectionReference
                .whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
                .whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
                .whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
                .whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());

但这只有在所有 4 个中都存在当前 ID 时才会显示。有没有更简单的方法可以做到这一点。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    您可以通过使用一个包含您要测试的 uid 的数组的字段,然后在其上应用 array-contains 来做到这一点。在你的情况下:

    在你的情况下:

    customer: [customer1, customer2, customer3, customer4]
    
    collectionReference
    .where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
    

    【讨论】:

    • 但这不会只搜索那个特定的文档吗?例如 final Query userQuery = collectionReference .whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid()) 将检查所有文档,并且无论它匹配的位置都会显示对于客户:[customer1,customer2,customer3, customer4] 不会只针对特定文档执行此操作吗?
    • 此查询将搜索给定集合中的所有文档,其字段“customer”是包含参数中的 uid 的数组。
    【解决方案2】:

    Firestore 不支持多个字段之间的逻辑 OR 查询。因此,使用您现在拥有的数据库结构进行单个查询是不可能实现的。您必须在客户端执行多个查询并合并结果。

    如果您希望能够使用单个查询,则必须更改您的数据库。一种选择是将所有客户放入单个数组字段中,并使用array-contains 查询在该字段中查找客户。

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 2018-04-11
      • 2018-03-30
      • 2021-04-27
      相关资源
      最近更新 更多