【问题标题】:Is there a way to query Firestore for a field that is either null or is an array that contains a certain value?有没有办法在 Firestore 中查询为空或包含特定值的数组的字段?
【发布时间】:2021-08-13 00:48:27
【问题描述】:

在我的例子中,我想检索集合中的所有文档,这些文档的字段“users”为空或包含特定用户 ID 的数组,例如“userId123”。

我知道我可以通过这样的查询自己实现一个或另一个:

FirebaseFirestore.instance.collection('documents').where('users', isEqualTo: null);

FirebaseFirestore.instance.collection('documents').where('users', arrayContains: 'userId123');

但是当我尝试像这样组合它们时,它不起作用:

FirebaseFirestore.instance.collection('documents').where('users', arrayContains: 'userId123').where('users', isEqualTo: null);

我知道我可以单独执行这些查询并在客户端将它们组合起来,但这似乎应该是可行的。

谢谢!

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    当您在一个查询中有多个 where 子句时,它们会被 AND'ed 在一起,因此您通过两个 where 调用获得的行为是预期的。

    我正在想办法实现您想要的查询,但不知何故没有想到。如果我想到了什么,我会更新——当然我希望其他人也能想到另一种方法。

    【讨论】:

    • 是的,现在我正在阅读文档,上面写着:“Cloud Firestore 为逻辑 OR 查询提供有限的支持。in 和 array-contains-any 运算符最多支持逻辑 OR 10 个相等 (==) 或数组包含单个字段的条件。对于其他情况,请为每个 OR 条件创建单独的查询,并在您的应用程序中合并查询结果。”来自firebase.google.com/docs/firestore/query-data/…。所以我想我只会在我的应用程序中合并查询。不过谢谢!
    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多