23

I would like to get all persons from the database where first name and last name is given by user input.

So far this is my code:

admin.database().ref('persons').orderByChild('Firstname').equalTo(firstName).limitToLast(1).once("value").then(function(snapshot) {
}

This code works filtering only Firstname, but I cant find any way of adding another Where Clause for Last name. I tried to add another orderBy but it does not seem working. Does the Realtime Database has to be this hard to make a simple query?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mizlul
  • 1
  • 7
  • 41
  • 100
  • The Firebase Realtime Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property, e.g. `"FirstName_LastName": "Frank_van Puffelen"`. For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Dec 31 '17 at 00:50

1 Answers1

21

The short answer: you can't. The long answer: use Google's new Firestore database or perform the filtering on the client manually. See this answer for other options.

SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
  • 13
    then I can say whoever made Realtime Database it sucks! thanks for the answer! – Mizlul Dec 30 '17 at 21:03
  • 1
    Yes you can...with a custom index. https://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – charlietfl Dec 30 '17 at 21:07
  • 1
    That's not true, if you read the answer it says you can't. `indexOn` is simply a way to improve read performance when sorting on an arbitrary child. – SUPERCILEX Dec 30 '17 at 21:17
  • 1
    @SUPERCILEX If you add a synthetic property to the data, you can accomplish the same use-cases in the realtime database as you can with Firestore. I'm all for recommending Firestore since it makes this a lot easier by managing the composite index for you, but Charlie is right that it **is** possible to filter on multiple values (at least in a simple case such as this one). – Frank van Puffelen Dec 31 '17 at 00:49
  • Oh yeah, forgot about that. Still, the question was asking about adding another `equalTo` which isn't possible. In any case, thanks for pointing it out! – SUPERCILEX Dec 31 '17 at 01:16