简体   繁体   English

两个时间戳之间的 Firebase Firestore 查询

[英]Firebase Firestore Query Between Two Timestamps

I want to find events that are a on now and upcoming (next 30 days) but that are also not in the past.我想找到现在和即将发生的事件(未来 30 天),但也不是过去的事件。

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();

Edit: 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.每个文档(事件)上都有一个 startDate 和 endDate 时间戳。

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.您可以做的是执行两个查询,在客户端合并结果,并执行最终过滤器以获得精确匹配。

  1. Make an assumption about the maximum duration for an event.假设事件的最长持续时间。 Call that amount of time "X".称这段时间为“X”。
  2. Query for all documents where startDate is greater than now - X, and also less than now + 30 days.查询 startDate 大于现在 - X 并且还小于现在 + 30 天的所有文档。 Call this result set "A".将此结果集称为“A”。
  3. Query for all documents where endDate is greater than now, and also less than now + 30 days.查询 endDate 大于现在且小于 now + 30 天的所有文档。 Call this result set "B".将此结果集称为“B”。
  4. On the client, iterate all the results from A and B, checking to see if the start and end dates fit the criteria you want.在客户端上,迭代 A 和 B 的所有结果,检查开始日期和结束日期是否符合您想要的条件。

I can't think of a way to structure your data that will do this with a single query.我想不出一种结构化数据的方法,可以通过单个查询来完成此操作。

From the docs:从文档:

You can only perform range comparisons (<, <=, >, >=) on a single field, and you can include at most one array-contains or array-contains-any clause in a compound query:您只能对单个字段执行范围比较(<、<=、>、>=),并且您最多可以在复合查询中包含一个 array-contains 或 array-contains-any 子句:

citiesRef.where("state", ">=", "CA").where("state", "<=", "IN");
citiesRef.where("state", "==", "CA").where("population", ">", 1000000);

https://firebase.google.com/docs/firestore/query-data/queries https://firebase.google.com/docs/firestore/query-data/queries

I know this is kind a old thread, but the answer might be good for others.我知道这是一个旧线程,但答案可能对其他人有好处。

What you can do as I at the end ended up doing, is that you have a $start_date and a $target_date.正如我最后所做的那样,你可以做的是你有一个 $start_date 和一个 $target_date。

You then do like this:然后你这样做:

<?php
$start = strtotime('2021-11-22');
$target = strtotime('2022-01-01 0:00:00');

$limit = (($target - $start) / 86400);

$query = $col_data->where('date.day_start', '>=', $start);
$query = $query->limit($limit);

?>

Not bad, eh?还不错吧? You welcome!没关系!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM