【问题标题】:How to get documents from Firestore where a field which is an array matches a string in Kotlin?如何从 Firestore 中获取文档,其中数组字段与 Kotlin 中的字符串匹配?
【发布时间】:2021-08-05 09:24:27
【问题描述】:

我想从Firestore 获取所有文档(产品),然后在recyclerVieww 中显示它们。标准是我拥有的值应该与“可交付成果”中的值匹配,其中可交付成果是一个数组。 因此,从集合“产品”中获取所有文档,其中“可交付”字段中的值与我提供的值匹配。 我尝试了以下方法,但没有得到预期的任何结果。我做错了吗?

有人可以帮我吗?

    fun getProductsDeliverable(deliverable: String) {
          mFireStore.collection("products")
            .whereEqualTo("deliverable_mun_pan", deliverable).get().addOnCompleteListener {
                if (it.isSuccessful) {
                    for (document in it.result) {

                        val product = document.toObject(Product::class.java)
                        product.product_id = document.id
                        productsList.add(product)

                    }
                } else {

                    Log.d("TAG", "There are no items")
                }

                srProductsList.addAll(srProductsList)
                successProductsList(srProductsList)

            }

【问题讨论】:

    标签: kotlin google-cloud-firestore


    【解决方案1】:

    您所描述的内容在 Firestore 中称为 array membership query,可以使用

    mFireStore.collection("products")
              .whereArrayContains("deliverable", "Pune")
    

    所以这匹配 deliverable 数组包含值 "Pune" 的产品。当然,您也可以指定 deliverable 变量,而不是硬编码值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2014-02-06
      • 2020-10-16
      • 2021-06-29
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      相关资源
      最近更新 更多