您能否在Firestore中的一个查询中将多个whereEqualTo操作链接在一起 [英] Can you chain multiple whereEqualTo operations in one query in pieces in Firestore

查看:47
本文介绍了您能否在Firestore中的一个查询中将多个whereEqualTo操作链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个多选对话框,其中包含用户可以选择的各种过滤器选项,以便根据文档的 rarity 字段过滤数据库.由于过滤器对话框中有许多选项,因此,如果我们考虑过滤器的所有可能组合,则手工覆盖每种情况会花费很多时间.考虑到这一点,我尝试创建一个起始查询,如下所示,然后遍历用户选择的过滤器列表,并尝试添加 whereEqualTo("rarity",filter)操作查询每个过滤器.我注意到您不能像普通变量那样连接查询 var i + = 5 ,所以我想知道是否有解决此类问题的方法.您是否可以在 相同查询中以分步/段方式 实际使用多个 whereEqualTo 操作,而不会覆盖以前的操作在同一查询上应用了操作?

In my app, I have a multiple-choice dialog with various filter options that the user should be able to choose in order to filter the database based on the rarity field of the documents. Since there are many options in the filter dialog, covering each case by hand would take ages if we take into account all the possible combinations of the filters. With that in mind, I tried creating a starting query as you can see below and then I iterate through the list of filters selected by the user and try to add a whereEqualTo("rarity",filter) operation to the query for each filter. I noticed that you can't concatenate queries like with normal variables e.g. var i += 5 so i would like to know if there is any solution to this kind of issue. Can you actually apply multiple whereEqualTo operations in the same query in steps/pieces without overriding the previously applied operations on that same query?

这是在收到用户在我的FilterActivity.kt类中选择的过滤器后尝试的操作:

Here's what I've tried after receiving the filters selected by the user in my FilterActivity.kt class:

class FilterActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_filter)

        val db = FirebaseFirestore.getInstance()

        val filters:ArrayList<String>? = intent.getStringArrayListExtra("filterOptions")
        Log.d("FilterActivity", "filter options $filters")

        var query = db.collection("Cards").orderBy("resID")

        for(filter in filters!!) {
            query = query.whereEqualTo("rarity",filter)
        }

        query.get().addOnSuccessListener { querySnapshot ->
            if(querySnapshot.isEmpty) Log.d("FilterActivity","is empty")
            for(doc in querySnapshot.documents) {
                Log.d("FilterActivity", "${doc.getString("name")} - ${doc.getString("rarity")}")
            }
        }
    }
}

推荐答案

基本上,您正在尝试执行 OR 操作,在该操作中,您将检索所有文档,其中稀有字段与其中的任何值匹配数组.

Basically you are trying a do OR operation, where you are retrieving all documents, in which rarity fields matched any of the value in array.

您正在尝试使用新的Firebase whereIn 操作,您可以在其中传递值数组,但过滤器中最多只能包含 10 个值

You are try new firebase whereIn operation where you can pass array of values, but theres a limitation of max 10 values in filter

 FirebaseFirestore.getInstance()
            .collection("Cards")
            .orderBy("resID")
            .whereIn("rarity",filters!!.toList())
            .get().addOnSuccessListener { querySnapshot ->
                if (querySnapshot.isEmpty) Log.d("FilterActivity", "is empty")
                for (doc in querySnapshot.documents) {
                    Log.d("FilterActivity", "${doc.getString("name")} - ${doc.getString("rarity")}")
                }
            }

过滤器arraylist最多可以包含10个值

filters arraylist can contain max 10 values

这篇关于您能否在Firestore中的一个查询中将多个whereEqualTo操作链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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