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

查看:61
本文介绍了使用< =&的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))

但我收到此错误:

Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field.

我认为该查询可与复合索引一起使用,但由于错误提示无法正常工作.

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.

推荐答案

您将不得不选择一个字段并查询该字段,然后在客户端过滤掉另一个字段. 复合查询文档对此是坚定的.

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天全站免登陆