在StartDate和EndDate Firestore之间 [英] Between StartDate and EndDate Firestore

查看:90
本文介绍了在StartDate和EndDate Firestore之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带startDateendDate的文档.我想在startDateendDate的范围之间查询.我可以像这样查询一个日期范围:

I have documents with a startDate and endDate. I would like to query between the range of the startDate and endDate. I can query between a range for one date like so:

whereField("startDate", isGreaterThan: start).whereField("startDate, isLessThan: end)

但是我不能像这样查询两个字段:

But I cannot query two fields like so:

whereField("startDate", isGreaterThan: start).whereField("endDate", isLessThan: end)

在复合where语句中使用多个字段时,Firestore会通过异常.

Firestore throughs an exception when using more than one field in a compound where statement.

推荐答案

无论您使用什么平台来构建应用程序,我都可以肯定所得到的错误是非常明显的.关于该主题,Cloud Firestore官方文档也非常明确.因此,关于Firestore,有一些查询限制:

No matter what is the platform that you are using for building your application, I'm sure that the error that you get is quite explicit. Cloud Firestore official documentation is also quite explicit regarding this topic. So there are some query limitations, when it comes to Firestore:

Cloud Firestore不支持以下类型的查询:

Cloud Firestore does not support the following types of queries:

  • 逻辑或查询.在这种情况下,您应该为每个OR条件创建一个单独的查询,然后将查询结果合并到您的应用中.

因此,Firestore查询只能对单个属性执行范围过滤.由于您尝试过滤两个属性上的范围,因此您正在处理该错误消息,这是预期的行为,因为在单个Firestore查询中无法做到这一点.

So a Firestore query can perform a range filtering only on a single property. Since you are trying to filter ranges on two properties, you're geeting that error message and this is the expected behaviour since this is not possible in a single Firestore query.

要解决此问题,您可以从以下解决方案之一中进行选择:

To solve this, you can choose from one of the following solutions:

  • 您可以对查询中的一个( first )字段和客户端的另一(第二个)字段进行过滤,正如官方文档所指出的那样.

  • You can perform filtering on one (first) field in the query and on the other (second) field client-side, as also the official documentation indicates.

您可以通过某种方式将两个范围的值组合到单个字段中,以使用例具有单个字段.这种组合的一个非常成功的例子是使用地理哈希过滤纬度和经度属性,正如Frank van Puffelen在此视频

You can combine the values of the two range into a single field in some way that allows your use-case with a single field. A very successful example of such a combination would be the use of geohashes for filtering on latitude and longitude properties as Frank van Puffelen explained very well in this video, Querying Firebase and Firestore.

另一种选择是更改存储数据的方式并以不同的方式对其进行建模.最简单的实现是将startDateendDate中的所有项目放入单个集合中.由于您没有为平台选择标签,因此我将用Javascript编写必要的查询,但是对于其他编程语言也可以简单地编写该查询.因此,您可以使用以下查询来查询该集合:

Another option is to change the way your are storing your data and model it differently. The most simple implementation would be to put all items within the startDate and endDate into a single collection. Since you didn't choose a tag for a platform, I will write the necessary query in Javascript but it can be simply written also for other programming languages. So you can query that collection with the following query:

  db.collection("startDate-endDate").where('date','>=', start).where('date','<=', end);

  • 另一个更通用的选择是将您需要的每个时期(年,月或日)分别存储在集合中的所有项目,然后执行必要数量的查询以获取您要查找的项目对于每个集合之一itemsFromFirstYearitemsFromSecondYear等.

    还请查看有关以下内容的官方文档:

    Please take also take a look at the official documentation regarding:

    • document on compound queries and their limitations
    • video on Cloud Firestore queries.

    恕我直言,我建议选择第一个选项.

    IMHO, I'd recommend picking up the first option.

    这篇关于在StartDate和EndDate Firestore之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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