Codist
Codist

Reputation: 769

How to get documents from Firestore where a field which is an array matches a string in Kotlin?

I want to get all documents (products) from the Firestore and then show them in the recyclerVieww. The criterion is that the value that I have should match with the one in the 'deliverable', where the deliverable is an array. So, get all the documents from the collection 'products' where the value in the field 'deliverable match the one I provide. I have tried the following but I don't get any result as I expected. Am I doing it wrong?

Can someone help me with it?

    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)

            }

enter image description here

Upvotes: 1

Views: 178

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

What you're describing is known as an array membership query in Firestore, and can be done with

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

So this matches products where the deliverable array contains a value of "Pune". Instead of the hard-coded value, you can also specify your deliverable variable of course.

Upvotes: 1

Related Questions