两个时间戳之间的 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屋!