简体   繁体   English

如何 select 文档在 Firestore 子集合中超过某个日期?

[英]How to select documents in a firestore subcollection that are past a certain date?

I have the following subcollection merchants/id/bookings/date Here, 'bookings' is the subcollection and 'date' is the name of a document which contains bookings for that date.我有以下子集合merchants/id/bookings/date这里,'bookings' 是子集合,'date' 是包含该日期预订的文档的名称。 Here's an example: merchants/a9sd7f692934/bookings/25-07-2020这是一个示例: merchants/a9sd7f692934/bookings/25-07-2020

(1) in my 'where' function, how to define the name of the document? (1)在我的'where'function中,如何定义文件名?

(2) this involves dates and I am using moment for this. (2) 这涉及日期,我正在为此使用时刻。 So how to transform a date that comes from firebase inside of the 'where' function?那么如何在“where”function 内部转换来自 firebase 的日期?

I would like to get all of the documents that have a date > currentDate and here's what I'm trying but don't know how to proceed我想获取所有具有日期 > 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())
            }
          })

I think you're looking for FieldPath.documentId : https://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#documentid我认为您正在寻找FieldPath.documentIdhttps://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#documentid

But the way you currently represent dates into the document ID does not lend itself for range operations.但是您当前在文档 ID 中表示日期的方式不适合范围操作。 Say you want all documents in July, there's no way to filter 25-07-2020 on that.假设您想要 7 月份的所有文件,则无法过滤25-07-2020

You'll need to name your documents in most important to least important order, so: 2020-07-25 .您需要按照从最重要到最不重要的顺序命名您的文档,例如: 2020-07-25 Then you can query for:然后可以查询:

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.

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