2

I have my nodes in Firebase as:

users---
       |
      uid---
           |
           phone:
           name:

I am looking for the IN implementation here. I want to check which enteries from a list of phone numbers are present in my database and then, fetch those enteries.

In SQL databases, equivlent query may be:

select phone from users where phone IN ('phone1','phone2','phone3',...)

How can I achieve this in my Android Firebase project?

Hashir Sarwar
  • 1,115
  • 2
  • 14
  • 28

3 Answers3

3

There is no way to replicate the following SQL query:

select phone from users where phone IN ('phone1','phone2','phone3',...)

into a Firebase realtime database query. What can you to instead, is to query the database for each phone number separately and collect the results client side.

If you consider at some point to try using Cloud Firestore, you can call get() method on each Query object and you'll see that it will return a Task object that it will become resolved when the document is ready. After that, simply collect all the tasks into a List of tasks, and pass that list to Tasks's whenAllComplete() method to respond when the entire set of tasks is complete. You can then check the results of all the tasks there, and take some actions accordingly.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
1

As Alex says, you can't query for a list of phones, instead, you can just query for one with equalsTo()

See Querying Data

https://firebase.google.com/docs/database/admin/retrieve-data

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
1

On Firestore, you can do a where clause, you don't need to specify individual entries:

var citiesRef = db.collection("cities");
var query = citiesRef.where("phone", "==", "phone1")

See:

Simple and compound queries

Sergio Flores
  • 439
  • 2
  • 8