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

查看:43
本文介绍了在 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 查询.在这种情况下,您应该为每个 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:

  • 您可以在查询中的一个(第一个)字段和客户端的另一个(第二个)字段上执行过滤,正如官方文档所示.

  • 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.

您可以通过某种方式将两个范围的值组合到一个字段中,从而允许您的用例使用单个字段.这种组合的一个非常成功的例子是使用 geohashes 过滤纬度和经度属性,正如 Frank van Puffelen 在此视频中很好地解释的那样,查询 Firebase 和 Firestore.

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:

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

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

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

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