带有 <= & 的 Firestore 复合查询>= [英] Firestore compound query with <= & >=

查看:15
本文介绍了带有 <= & 的 Firestore 复合查询>=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Firestore 集合中查询开始时间(这是一个数字)为 >= slot.start<= slot.end 的条目>.

I need to query a firestore collection for entries where a start time (which is a number) is >= slot.start and and <= slot.end.

像这样:

.collection('Entries', ref => ref.where('timeStart', '>=', slot.timeStart).where('timeEnd', '<=', slot.timeEnd))

但我收到此错误:

无效查询.具有不等式(<、<=、> 或 >=)的所有 where 过滤器必须位于同一字段中.

我认为这个查询可以与复合索引一起使用,但正如错误所说,这不起作用.

I thought this query would work with composite index but as the error says that does not work.

当我将查询更改为:

.collection('Entries', ref => ref.where('timeStart', '==', slot.timeStart).where('timeEnd', '<=', slot.timeEnd))

我可以为它创建一个索引.此外,当我将第二个 '<=' 更改为 '==' 时,我什至不需要它的索引.

I can create an index for it. Furthermore when I change the 2nd '<=' to an '==' aswell I don't even need an index for it.

我不认为我的查询那么高级,我对 firestore 无法执行此查询感到有点失望.那么我的问题的最佳解决方案是什么?像这样省略第二个查询语句是下一个最好的事情

I didn't think my query is that advanced and I'm a little bit disappointed that firestore can't do this query. So what is the best solution for my problem? Is the next best thing to omit the 2nd query statement like this

.collection('Entries', ref => ref.where('timeStart', '>=', slot.timeStart))

然后遍历结果并检查 timeEnd <= entry.timeEnd 是否手动"?这对我来说似乎是一个非常糟糕的解决方案,因为那时我可能需要加载大量数据.

And then iterate through the results and check if timeEnd <= entry.timeEnd "manually"? That seems like a really bad solution for me because I may need to load a lot of data then.

推荐答案

您将不得不选择一个字段并查询该字段,然后在客户端过滤掉另一个.Compound Queries 文档对此有明确的说明.

You're going to have to pick one field and query that one then filter out the other on the client side. Compound Queries documentation is firm on this one.

您只能对单个字段执行范围比较(<、<=、>、>=),并且您最多可以在一个字段中包含一个 array_contains 子句复合查询:

You can only perform range comparisons (<, <=, >, >=) on a single field, and you can include at most one array_contains clause in a compound query:

这篇关于带有 &lt;= &amp; 的 Firestore 复合查询&gt;=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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