繁体   English   中英

查询 firestore 中的日期字段不起作用

[英]query on date field in firestore not working

我是编程新手,只是作为一种爱好,通常如果我遇到问题,我可以在 inte.net 的某个地方找到答案,但是花了 2 天的大部分时间仍然无法弄清楚这个问题。 我正在使用 kotlin 和 android 工作室编写 android 应用程序。 第一个问题问,请温柔点!

我在 Firestore 中保存了一个日期,作为使用此代码保存到 hashmap 的时间戳

    val dateFormat = SimpleDateFormat("dd-MM-yyyy")
    val date = findViewById<TextView>(R.id.textMatchDate).text.toString()
    val timeStamp = Timestamp(dateFormat.parse(date)

然后将时间戳保存到 firestore。

我的检索代码是

        val date = "01-01-2020"
        val date1 = dateFormat.parse(date)
        val startDate = Timestamp(date1)

val db = FirebaseFirestore.getInstance()
        db.collection("snookerResults")
            .whereGreaterThan("date", startDate)
            .whereEqualTo("homeTeam", team)
            .get()
            .addOnSuccessListener {...}

为简单起见,我将获取startDate的方式设置为 0f,它是通过编程方式派生的,但是格式相同的字符串。 尽管知道应该返回结果,但我得到一个空数组。 如果我注释掉whereGreaterThan行,我会返回所有结果。

当我正常运行时,我有日志输出来说明问题我得到这个 output:-

2020-08-29 23:27:04.858 17112-17112/com.example.aggregatecalculator I/障碍:选择的球队是不伦瑞克

2020-08-29 23:27:05.086 17112-17112/com.example.aggregatecalculator I/障碍:开始日期是时间戳(秒=1577836800,纳秒=0)

如果我注释掉whereGreaterThan行,那么我会得到这个 output

2020-08-29 23:37:08.331 18164-18164/com.example.aggregatecalculator I/handicap:选择的球队是不伦瑞克

2020-08-29 23:37:08.582 18164-18164/com.example.aggregatecalculator I/障碍:开始日期是时间戳(秒=1577836800,纳秒=0)

2020-08-29 23:37:08.583 18164-18164/com.example.aggregatecalculator I/handicap: 日期是 [Wed Jan 01 00:00:00 GMT+00:00 2020, Wed Sep 30 00:00:00 GMT +01:00 2020]

2020-08-29 23:37:08.793 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1583712000,纳秒=0)

2020-08-29 23:37:08.793 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1575244800,纳秒=0)

2020-08-29 23:37:08.793 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1569798000,纳秒=0)

2020-08-29 23:37:08.794 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1572825600,纳秒=0)

2020-08-29 23:37:08.794 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1582502400,纳秒=0)

2020-08-29 23:37:08.794 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1584921600,纳秒=0)

2020-08-29 23:37:08.795 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1568588400,纳秒=0)

2020-08-29 23:37:08.795 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1574035200,纳秒=0)

2020-08-29 23:37:08.795 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1571612400,纳秒=0)

2020-08-29 23:37:08.796 18164-18164/com.example.aggregatecalculator I/handicapresults:日期是时间戳(秒=1571007600,纳秒=0)

如您所见,时间戳是相同的格式,并且返回的 3 个时间戳大于开始日期,因此whereGreaterThan startdate

在此处输入图像描述

fun getNewHcap(team: String){
val players = arrayListOf<String>()
val db = FirebaseFirestore.getInstance()
db.collection("SnookerPlayers")
    .whereEqualTo("league", "Big Table Monday")
    .whereEqualTo("team", team)
    .get()
    .addOnSuccessListener { task ->
        for (doclist in task){
            val player = doclist["player"].toString()
            players.add(player)
        }
        val dates = hcapMinMaxDate()
        val dateFormat = SimpleDateFormat("dd-MM-yyyy")
        val date = "01-01-2020"
        val date1 = dateFormat.parse(date)
        val startDate = Timestamp(date1)
        val endDate = Timestamp(dates[1])
        Log.i("handicap", "startdate is $startDate")
       // Log.i("handicap", "startdate is $endDate")

        val db = FirebaseFirestore.getInstance()
        db.collection("snookerResults")
            //.whereGreaterThan("date", startDate)
            .whereEqualTo("homeTeam", team)

           //.whereLessThan("date", endDate)
            .get()
            .addOnSuccessListener { task ->
                for (docList in task){
                   // val player = docList["home player 1"].toString()
                    val date = docList["date"].toString()

                    Log.i("handicapresults", "date is $date")
                }
            }


        Log.i("handicap", "date is $dates")

    }
Log.i("handicap", "chosen team is $team")

尽管知道应该返回一些结果,但我得到一个空数组。 如果我注释掉whereGreaterThan()行,我会返回所有结果。

由于以下代码行,您很可能得到一个空数组:

db.collection("snookerResults")
    .whereGreaterThan("date", startDate)
    .whereEqualTo("homeTeam", team)
    .get()
    .addOnSuccessListener {...}

这是因为在同一查询中调用.whereGreaterThan().whereEqualTo()需要一个索引。 该索引可以在您的Firebase 控制台中手动创建,或者如果您使用的是 Android Studio,您将在 logcat 中找到一条听起来像这样的消息:

FAILED_PRECONDITION: The query requires an index. You can create it here: ...

您只需单击链接或将 URL 复制并粘贴到 web 浏览器中,系统就会自动创建您的索引。 等待几分钟,直到创建索引,您将获得所需的结果。

但是,这种方法可能不是最好的方法,因为您需要为数据库中的每个 homeTeam 创建一个索引。 如果你有几个或固定数量的团队,它会起作用,但如果你有很多团队,它会很困难,在这种情况下,建议复制数据。 这种做法称为非规范化,在 Cloud Firestore 中是很常见的做法。 如果您是 NoSQL 数据库的新手,为了更好地理解,我建议您观看此视频, Firebase 数据库的非规范化是正常的 它适用于 Firebase 实时数据库,但同样的原则也适用于 Cloud Firestore。 除此之外,我想您可能会对我在以下帖子中的回答感兴趣:

Firebase Cloud Firestore 中的非规范化是什么?

因此,在您的特定情况下,我将创建一个如下所示的新集合:

Firestore-root
  |
  --- homeSnookerResults (collection)
         |
         --- homeTeamId (document)
               |
               --- allResults (collection)
                     |
                     --- resultIdOne (document)
                           |
                           --- //result object properties

在这种情况下,一个看起来像这样的查询就可以解决问题:

db.collection("homeSnookerResults")
    .document(homeTeamId)
    .collection("allResults")
    .whereGreaterThan("date", startDate)
    .get()
    .addOnSuccessListener {...}

在这种情况下,不需要索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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