whereArray包含限制为10 [英] whereArrayContains limit to 10

查看:103
本文介绍了whereArray包含限制为10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据 tagIDs 过滤 questionCollection .一切正常,但 whereArraycontains 最多可以使用10个ID.

I want to filter questionCollection on basis of tagIDs . Everything working fine but whereArraycontains is working for max 10 id's.

我该如何改善我的结构以使其能够工作超过10个tagIDs,并确保减少呼叫服务器的次数,以减少支出.

How could I improve my structure to work for more than 10 tagIDs and also make sure to call as less as hit to server to reduce money spend.

Firestore-root
   |
   --- questions (collection)
   |     |
   |     --- qid (documents)
   |          |
   |          --- title: "Question Title"
   |          |
   |          --- qid: "autogeneratedID"
   |          |
   |          --- tagIDs =[tagID_1,tagID_2...tag_IDs_5] // array
   |
   --- tags (collection)
   |     |
   |     --- tagID (documents)
   |          |
   |          --- tagName: "Mathematics"
   |          |
   |          --- tagID: "autogeneratedID"
   |

获取包含特定标记ID的问题(如果标记ID大于10,则失败)

fetch questions that contains specific tagIDs ( fails if tagIDs greater than 10)

private fun fetchQuestion(tagMap: HashMap<String, String>) {

        val tagIDList:MutableList<String> = ArrayList();
        for ((key) in tagMap) { 
            tagIDList.add(key);
        }
        var query: Query = db.collection(Constants.QUESTION_COLLECTION)
                .orderBy(Constants.KEY_QUESTION_ID, Query.Direction.DESCENDING).limit(50)
        if(!tagIDList.isNullOrEmpty())
            query = query.whereArrayContainsAny("tagIDs", tagIDList);//if list greater than 10 it's not working

        query.get().addOnSuccessListener { queryDocumentSnapshots ->
                if(!queryDocumentSnapshots.isEmpty){
                    for (documentSnapshot in queryDocumentSnapshots) {
                        val question = documentSnapshot.toObject(QuestionBO::class.java)
                    }
                }
            }.addOnFailureListener { e ->
                toast("No record found.")
                e.printStackTrace()
            }
    }

推荐答案

whereArrayContains查询的文档非常具体.它只能与10个数组项一起使用:

The documentation for whereArrayContains queries is very specific. It can only work with 10 array items:

使用array-contains-any运算符将同一字段上的最多10个array-contains子句与逻辑OR

use the array-contains-any operator to combine up to 10 array-contains clauses on the same field with a logical OR

您应该知道whereArrayContains不会减少计费文档读取的次数.如果您的阵列包含10个项目,则仍将花费10次文档读取.如果执行10个whereArrayContains查询,每个查询包含10个数组项,则仍将花费100次读取.

You should know that whereArrayContains does not reduce the number of billed document reads. If your array contains 10 items, it will still cost 10 document reads. If you perform 10 whereArrayContains queries, each with 10 array items, it will still cost 100 reads.

如果您需要N个文档,则没有捷径可以使N个文档的读取费用少于N个文档的读取费用.

If you need N documents, there is no shortcut to make those N document reads cost less than the cost of N document reads.

这篇关于whereArray包含限制为10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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