两个时间戳之间的Firebase Firestore查询 [英] Firebase Firestore Query Between Two Timestamps
问题描述
我想查找现在和即将发生(接下来的30天)但又不是过去的事件.
I want to find events that are a on now and upcoming (next 30 days) but that are also not in the past.
当我将此功能作为云函数运行时,出现在多个属性上不能具有不等式过滤器".我要如何获取此类数据.
When i run this as a cloud function, I get "Cannot have inequality filters on multiple properties". How am I meant to get this type of data.
(忽略日期内容有点混乱,仍然在玩耍的事实).
(ignore the fact that the date stuff is a bit messy, am still playing around).
// Create date 30 days in future
const searchData: Date = new Date();
searchData.setDate(searchData.getDate() + 30);
// Load data and handle empty respoonse
const response: admin.firestore.QuerySnapshot = await admin
.firestore()
.collection(Collections.EVENTS)
.where("startDate", "<=", admin.firestore.Timestamp.fromMillis(searchData.valueOf()))
.where("endDate", ">=", admin.firestore.Timestamp.fromMillis(new Date().valueOf()))
.where("public", "==", true)
.limit(NUMBER_OF_EVENTS)
.get();
我想知道数据结构/查询方法,该方法将允许我返回现在或在下个月开始的事件集合中的所有事件.另外,我希望查询排除已经完成的事件.每个文档(事件)上都有一个startDate和endDate时间戳.
I would like to know the data structure/query method that will allow me to return all events in the events collection that are on now or that will start in the next month. Additionally, I would like the query to exclude events that have already finished. Each document (event) has a startDate and endDate timestamp on it.
推荐答案
由于您有两个要检查范围的字段,因此我不确定这对于单个查询是否可行.相反,您可以执行两个查询,在客户端上合并结果,然后执行最终过滤器以获取完全匹配.
Since you have two fields to check with ranges, I'm not sure this is doable with a single query. What you can do instead is perform two queries, merge the results on the client, and perform a final filter to get the exact matches.
- 对事件的最大持续时间进行假设.将该时间称为"X".
- 查询所有startDate大于现在-X且小于现在+ 30天的文档.将此结果集称为"A".
- 查询endDate大于现在且小于现在+ 30天的所有文档.将此结果集称为"B".
- 在客户端上,迭代A和B的所有结果,检查开始日期和结束日期是否符合您想要的条件.
我想不出一种方法来构造您的数据,只需一个查询就可以做到这一点.
I can't think of a way to structure your data that will do this with a single query.
这篇关于两个时间戳之间的Firebase Firestore查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!