android - 从 Firestore 获取数据,其中数组与 Kotlin 中的数据列表匹配

标签 android firebase kotlin google-cloud-firestore

以下是我的 Firestore 数据库的结构。我想从“products”集合中获取所有文档,其中“pin_code”(是一个数组)与我拥有的 pin 代码列表匹配。 PIN 码列表来自“地址”集合,我通过以下代码设法获得了该集合。但我无法从“产品”集合中获取与 pin 码列表匹配的文档。

以下是我必须从“地址”集合中获取 pin 码列表的代码

        fun getPins(context: DashboardFragment) {

        mFireStore.collection("addresses")
            .whereEqualTo("user_id", getCurrentUserID())
            .get()
            .addOnSuccessListener { document ->

                val codeList: MutableList<String> = mutableListOf()

                for (i in document.documents) {

                    val code = i.toObject(Address::class.java)
                    code!!.user_id = i.id

                    codeList.add(code.pinCode)

                }

                context.getProductListBasedOnPin(codeList)

            }
            .addOnFailureListener { e ->

            }

    }

我尝试使用以下代码从集合“产品”中获取数据。但使用此代码,仅当我的“pin_code”不是数组时,我才能获取产品列表。但由于某种原因,我不得不将 pin_code 设为数组,并且无法获取产品列表。

    fun getProductListBasedOnPin(pinList: List<String>?) {
    val mFireStore = FirebaseFirestore.getInstance()

    mFireStore.collection("products")
        .whereIn("pin_code", pinList!!)
        .get()
        .addOnSuccessListener { document ->

            for (i in document.documents) {

                val product = i.toObject(Product::class.java)!!
                product.product_id = i.id
                srchProductsList.add(product)

            }

            srchTempProductsList.addAll(srchProductsList)

            if (newView == "ListView") {

                successDashboardItemsListListView(srchTempProductsList)
            } else {
                successDashboardItemsList(srchTempProductsList)
            }

        }
        .addOnFailureListener {

        }
}

有人可以帮我解决这个问题吗?

谢谢。

enter image description here

最佳答案

I want to get all the documents from the collection 'products' where the 'pin_code' (is an Array) matches with the list of pin codes I have.

您绝对可以使用 Query 的 whereArrayContainsAny(String field, List<? extends Object> values) 方法来做到这一点,该方法:

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.

假设您想要从“products”集合中获取所有文档,其中“pin_code”数组包含一个具有两个值(“123456”和“159874”)的列表,请使用以下代码行:

val pinCodeList = listOf("123456", "159874")
productsRef.whereArrayContainsAny("pin_code", pinCodeList).get().addOnCompleteListener {
    if (it.isSuccessful) {
        for (document in it.result) {
            Log.d(TAG, document.id + " => " + document.data)
        }
    } else {
        Log.d(TAG, "Error getting documents: ", task.exception)
    }
}

关于android - 从 Firestore 获取数据,其中数组与 Kotlin 中的数据列表匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68552732/

相关文章:

java - onLocationChanged 方法仍在不同的 Activity 中运行

json - Kotlin 没有将 "is"作为 json key 的开始

sockets - 使用Kotlin + Sockets +协程的标准方式是什么?

java - 来自 parse.com 的空值查询

Android .xml设计

javascript - Firebase 与 Web : Using user Id in reference causes error: Uncaught TypeError: Cannot read property 'on' of undefined

javascript - 如何删除 Firebase 实时数据库中的值?

android - Kotlin : FirebaseMessigingService is recive the message, 但未显示通知

android - 如何在主 Activity 中隐藏标题栏 : I'm Using Android Studio

FirebaseRecyclerAdapter - populateViewHolder 没有在第一次运行时填充数据?