【问题标题】:How to query firestore for documents with a timestamp field?如何在firestore中查询带有时间戳字段的文档?
【发布时间】:2020-04-17 14:53:38
【问题描述】:

我想获取 START_DATE 在过去 10 天内的所有文档。 例如,如果今天是 17-04-2020(DD-MM-YYYY),那么我想获取 START_DATE 7-04-2020 或以上的文档 我的 Collection 结构如下所示:

我需要在 Java 中为我的 android 应用程序执行此操作。

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    如果要查询集合中的时间戳字段,可以使用带有范围过滤器的 java Date 或 Firestore Timestamp 对象。

    db
        .collection("POLL")
        .whereGreaterThan("START_DATE", date1)
        .whereLessThan("START_DATE", date2)
    

    您必须提供满足您需求的 date1date2 值。

    【讨论】:

      【解决方案2】:

      我认为您还没有阅读文档,请在获得解决方案后检查一下: https://firebase.google.com/docs/firestore/query-data/queries

      根据您的要求使用带条件的 where 查询。

      例如:

          db.collection("cities")
                  .whereEqualTo("capital", true)
                  .get()
                  .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                      @Override
                      public void onComplete(@NonNull Task<QuerySnapshot> task) {
                          if (task.isSuccessful()) {
                              for (QueryDocumentSnapshot document : task.getResult()) {
                                  Log.d(TAG, document.getId() + " => " + document.getData());
                              }
                          } else {
                              Log.d(TAG, "Error getting documents: ", task.getException());
                          }
                      }
                  })
      
      ;
      

      编辑

      https://maye.pwafire.org/articles/using-timestamp-to-filter-and-order-firebase-cloud-firestore-documents/

      【讨论】:

      • 我已阅读文档。我没有发现任何关于使用时间戳字段查询的信息。
      • @smit 试试我分享的链接
      猜你喜欢
      • 2020-07-10
      • 2020-12-26
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2020-12-14
      • 2020-02-08
      相关资源
      最近更新 更多