[英]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.