[英]Firebase Firestore Query Between Two Timestamps
我想找到現在和即將發生的事件(未來 30 天),但也不是過去的事件。
當我將它作為雲函數運行時,我得到“多個屬性上不能有不等式過濾器”。 我怎么想得到這種類型的數據。
(忽略日期內容有點亂的事實,我還在玩)。
// 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 時間戳。
由於您有兩個字段要檢查范圍,因此我不確定這對單個查詢是否可行。 您可以做的是執行兩個查詢,在客戶端合並結果,並執行最終過濾器以獲得精確匹配。
我想不出一種結構化數據的方法,可以通過單個查詢來完成此操作。
從文檔:
您只能對單個字段執行范圍比較(<、<=、>、>=),並且您最多可以在復合查詢中包含一個 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
我知道這是一個舊線程,但答案可能對其他人有好處。
正如我最后所做的那樣,你可以做的是你有一個 $start_date 和一個 $target_date。
然后你這樣做:
<?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);
?>
還不錯吧? 沒關系!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.