在firebase中查询列表 [英] Query over list in firebase

查看:177
本文介绍了在firebase中查询列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Firebase使用了以下自定义模型,我需要查询带有特定标签的城市,我尝试了以下代码,但没有用:(

I've the below custom model for firebase, I need to query for the cities with specific tags, I tried the below code but did not work :(

data class City(
        val name: String,
        val state: String?,
        val country: String,
        val capital: Boolean,
        val population: Long,
        val tags : List<String>
)

val sf = City("San Francisco", "CA", "USA",false,
            860000, listOf("tag 1", "tag 2", "tag 3"))

var cities = db.collection("cities")
cities.document("SF").set(sf)

val tags = "(tag 1)|(tag 5)".toRegex()
db.collection("cities")
            .whereEqualTo("name",tags)  // This is wrong 
            .get()
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    for (document in task.result) {
                        Log.d(TAG, document.id + " => " + document.data)
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.exception)
                }
            }

推荐答案

Firestore查询仅支持相等和范围运算.他们不支持正则表达式.您将必须执行两个查询,然后在客户端上合并结果.

Firestore queries only support equality and range operations. They don't support regular expressions. You will have to perform two queries and merge the result on the client.

请参阅:

  • How to perform compound queries with logical OR in Cloud Firestore?
  • Firestore Query Across Multiple Fields for Same Value

这篇关于在firebase中查询列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆