[英]Query firebase data by timestamp month
所以我在 firebase Cloud firestore 中有這樣的記錄:
[
{
"category":"drinks",
"expensetype":"card",
"id":"5c673c4d-0a7f-9bb4-75e6-e71c47aa8d1d",
"name":"JJ",
"note":"YY",
"price":57,
"timestamp":"2017-11-30T22:44:43+05:30"
},
{
"category":"drinks",
"expensetype":"card",
"id":"85731878-eaed-2740-6802-e35e7758270b",
"name":"VV",
"note":"TTT",
"price":40,
"timestamp":"2017-12-30T22:41:13+05:30"
}
]
我想查詢帶有時間戳的數據並獲取屬於特定月份或日期的數據。
例如,如果我在我的日歷中單擊一個特定的月份(比如三月),我只希望在我的結果中顯示該特定月份的數據。 我怎樣才能做到這一點?
我使用firebase.firestore.FieldValue.serverTimestamp()
保存了日期
在此先感謝您的幫助。
編輯:
這就是我形成查詢的方式
var spendsRef = this.db.collection('spend', ref => ref.where("timestamp", ">", "12-12-2017"));
this.items = spendsRef.valueChanges();
但它仍然返回數據庫中的所有數據
回答:
var todayDate = new Date("Tue Jan 02 2018 00:00:00 GMT+0530 (IST)") // Any date in string
var spendsRef = this.db.collection('spend', ref => ref.where("timestamp", ">", todayDate));
this.items = spendsRef.valueChanges();
使用兩個相對運算符,可以確定查詢返回的文檔的精確范圍:
ref.where("timestamp", ">=", "2017-11").where("timestamp", "<", "2017-12")
這是android方式
Query query = mFirestore.collection("rootcollection").whereEqualTo("month", 3);
您可以按時間戳進一步對結果進行排序
Query query = mFirestore.collection("rootcollection")
.orderBy("timestamp", Query.Direction.DESCENDING)
.whereEqualTo("month", 3);
我正在使用 node.js 並將一個參數存儲為new Date(new Date().toUTCString())
。
現在,我獲取相同的數據,對其進行一些處理,並在 where 查詢中使用參數作為new Date(<param received in fetch>)
在許多記錄中獲取相同的記錄。
謝謝@Syed Sehu Mujammil A,您的回答有效。
我通過以下方式解決了類似的問題:
var now = new Date(new Date().toUTCString());
const db = collection(fireStore, 'jobs_col');
return await getDocs(query(db, where('expire_date', '>=', now)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.