1

I want to find all the users whose phone number is contained in the list. It's like searching * from database where email IN phone_numbers.

This is what I am doing.

fun getUsers(persons: List<Person>?): Single<List<User>> {
    return Single.create({ emitter ->
        try {
            val db = FirebaseFirestore.getInstance()
            val userCollection = db.collection(USER_FIREBASE_TABLE)
            var query: Query? = null
            persons?.forEach({ person ->
                query = if (null == query) userCollection
                        .whereEqualTo("phone", person.phone)
                else query!!.whereEqualTo("phone", person.phone)
            })
            query?.get()?.addOnCompleteListener({ task: Task<QuerySnapshot> ->
                emitter.onSuccess(task.getResult().toObjects(User::class.java))
            })
        } catch (exception: Exception) {
            exception.printStackTrace()
            emitter.onError(exception)
        }
    })  
}

As you see I am appending whereEqualTo each time to the query. Is there any way to replace it with something like whereIn and I pass an array or collection of values to match to?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Debanjan
  • 2,817
  • 2
  • 24
  • 43
  • @Alex Mamo, now wherein is supported in firestore : https://firebase.googleblog.com/2019/11/cloud-firestore-now-supports-in-queries.html – sayvortana Feb 16 '20 at 00:00

1 Answers1

0

https://firebase.google.com/docs/firestore/query-data/queries#compound_queries

You can also chain multiple where() with equal operator ex.:

citiesRef.whereEqualTo("state", "CA").whereEqualTo("state", "CO");...

Additionally, you can only perform range comparisons (<, <=, >, >=) on a single field:

citiesRef.whereGreaterThanOrEqualTo("state", "CA").whereLessThanOrEqualTo("state", "IN")
Alex Mounir
  • 1,243
  • 1
  • 14
  • 20