如何在 Cloud Firestore 中使用逻辑 OR 执行复合查询? [英] How to perform compound queries with logical OR in Cloud Firestore?

查看:31
本文介绍了如何在 Cloud Firestore 中使用逻辑 OR 执行复合查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自文档:

您还可以链接多个 where() 方法来创建更具体的查询(逻辑 AND).

You can also chain multiple where() methods to create more specific queries (logical AND).

如何执行 OR 查询?示例:

How can I perform an OR query? Example:

  1. 给我字段 statusopenupcoming
  2. 的所有文档
  3. 给我字段 status == opencreatedAt <= <somedatetime>
  4. 的所有文档
  1. Give me all documents where the field status is open OR upcoming
  2. Give me all documents where the field status == open OR createdAt <= <somedatetime>

推荐答案

OR 不受支持,因为服务器很难对其进行扩展(需要将状态保持为重复数据删除).解决方法是发出 2 个查询,每个条件一个,并在客户端上进行重复数据删除.

OR isn't supported as it's hard for the server to scale it (requires keeping state to dedup). The work around is to issue 2 queries, one for each condition, and dedup on the client.

编辑(2019 年 11 月):

Cloud Firestore 现在支持 IN 查询,这是一种有限类型的 OR 查询.

Cloud Firestore now supports IN queries which are a limited type of OR query.

对于上面的例子,你可以这样做:

For the example above you could do:

// Get all documents in 'foo' where status is open or upcmoming
db.collection('foo').where('status','in',['open','upcoming']).get()

但是,仍然无法执行涉及多个字段的通用 OR 条件.

However it's still not possible to do a general OR condition involving multiple fields.

这篇关于如何在 Cloud Firestore 中使用逻辑 OR 执行复合查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆