firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

Query.where with inArray not working properly?

Open DavinRot opened this issue 1 year ago • 2 comments

So first off, my assumption about the Query.where method that takes params field: String, inArray: List<Any> is that it should be returning a list of all entries from that collection where the field matches an entry in that list (in my case the field is an id and I'm checking against a list of Strings). First off, please correct me if I'm just using the query wrong. Assuming I'm not, I can't get it to work - I keep getting an empty list, but using Query.where with field: String, equalTo: Any? and piecing the list together works fine. Is the method bugged, or am I just not using it right?

DavinRot avatar Sep 26 '23 17:09 DavinRot

It's definitely broken.

Tried the functions below. The common version doesn't work (returns zero results), but the ones where I use the native versions work as expected.

Tried looking at the source code, but as far as I can tell it should work.

I am using expect/actual as a workaround for now.

// common - Doesn't work (returns zero results)
actual suspend fun query(categories: List<String>): Query =
    Firebase.firestore.collection("categories")
        .where("name", inArray = categories)

// Android - works (returns seven results as expected)
actual suspend fun query(categories: List<String>): Query = Query(
    Firebase.firestore.collection("categories").android
        .whereIn("name", categories)
)

// iOS - works (returns seven results as expected)
actual suspend fun query(categories: List<String>): Query = Query(
    Firebase.firestore.collection("categories")
        .ios.queryWhereFieldPath(FIRFieldPath(listOf("name")), `in` = categories))

Qw4z1 avatar Oct 11 '23 13:10 Qw4z1

It looks like method fun Query.where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null) internally calls method internal fun _where(field: String, lessThan: Any? = null, greaterThan: Any? = null, arrayContains: Any? = null): Query instead of method internal fun _where(field: String, inArray: List<Any>? = null, arrayContainsAny: List<Any>? = null): Query. Internal method that uses array is not even being used.

andjela-aetherius avatar Dec 18 '23 13:12 andjela-aetherius

@Daeda88 has your changes to where clauses fixed this issue now?

nbransby avatar Apr 10 '24 05:04 nbransby

According to this test it should work https://github.com/GitLiveApp/firebase-kotlin-sdk/blob/2e736d437c19cd27a16da2f208e6338db66e13c8/firebase-firestore/src/commonTest/kotlin/dev/gitlive/firebase/firestore/firestore.kt#L954

Daeda88 avatar Apr 10 '24 06:04 Daeda88

Yeah, now it works, thank you

andjela-aetherius avatar Apr 10 '24 07:04 andjela-aetherius