1

We are building an android app that needs to synchronize phone contacts with people already registered on the app. We are using firebase

To do this, we'd like to retrieve a list of existing users based on their phone numbers.

I have managed to retrieve users based on their phone number with ref.orderByChild("phone").equalTo($phoneNumber)

But I am wondering if there is a way of passing a list of phone numbers, instead of querying for each phone number one at a time ? Something like this: ref.orderByChild("phone").isIn([phone1, phone2, phone3])

I am just beginning to learn Firebase but I love the concept :) Thanks a lot for your answers!

adrien
  • 41
  • 5

1 Answers1

1

Firebase doesn't have or or in operators on its queries.

The closest you can come with with the startAt and endAt functions, to select a range. But that doesn't work for your use-case.

Normally when people are asking for this type of operation, there is a relation between all the pieces that they're trying to combine in a query. For example in your case, the use-case is likely something like: "get the name for all contacts in the user's address book".

In such a situation there are a few options:

  1. monitor each contact with a separate query
  2. embed the necessary metadata for each contact into the user's address book

Option 2 is the cheapest way to get the information, because you only need to read the address book. But it comes at the cost of data duplication, which more relationally trained developers are unused to. See this answer for a coughgreatcough example of such denormalization: Firebase data structure and url

Option 1 is not nearly as expensive as you may expect, since Firebase will open a socket connection only once and then perform all additional queries over that same connection.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks a lot for your answer it's very clear :) I was working with MongoDB before and such query was possible that's why I was asking, but I'll use one of your solutions ! Maybe new query operators will come in the future :) – adrien Apr 26 '15 at 19:36