如何在 Cloud Firestore 中使用逻辑 OR 执行复合查询? [英] How to perform compound queries with logical OR in Cloud Firestore?
问题描述
来自文档:
您还可以链接多个 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:
- 给我字段
status
为open
或upcoming
的所有文档 - 给我字段
status == open
或createdAt <= <somedatetime>
的所有文档
- Give me all documents where the field
status
isopen
ORupcoming
- Give me all documents where the field
status == open
ORcreatedAt <= <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屋!