[英]How to select documents in a firestore subcollection that are past a certain date?
我有以下子集合merchants/id/bookings/date
這里,'bookings' 是子集合,'date' 是包含該日期預訂的文檔的名稱。 這是一個示例: merchants/a9sd7f692934/bookings/25-07-2020
(1)在我的'where'function中,如何定義文件名?
(2) 這涉及日期,我正在為此使用時刻。 那么如何在“where”function 內部轉換來自 firebase 的日期?
我想獲取所有具有日期 > currentDate 的文檔,這就是我正在嘗試但不知道如何繼續
//currentDate = 20-07-2020 //typeof String
firebase
.firestore()
.collection('merchants')
.doc(id)
.collection('bookings')
//??? -> how to get doc by its name? how to compare it to a moment date? how to convert it?
.where("???", ">", currentDate).get().then(docs => {
console.log(docs.data())
}
})
我認為您正在尋找FieldPath.documentId
: https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#documentid
但是您當前在文檔 ID 中表示日期的方式不適合范圍操作。 假設您想要 7 月份的所有文件,則無法過濾25-07-2020
。
您需要按照從最重要到最不重要的順序命名您的文檔,例如: 2020-07-25
。 然后可以查詢:
firebase
.firestore()
.collection('merchants')
.doc(id)
.collection('bookings')
.where(FieldPath.documentId(), ">", "2020-07-")
.where(FieldPath.documentId(), "<=", "2020-07-31")
.get().then(docs => {
docs.forEach((doc) => {
console.log(doc.data())
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.