0

I need to do a query where I can show only specific data using an 'AND' statement or equivalent to it. I have taken the example which is displayed in the Firebase Documentation.

// Find all dinosaurs whose height is exactly 25 meters.
var ref = firebase.database().ref("dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot)    {
console.log(snapshot.key);
});

I understand this line is going to retrieve all the dinosaurs whose height is exactly 25, BUT, I need to show all dinosaurs whose height is '25' AND name is 'Dino'. Is there any way to retrieve this information?

Thanks in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rodolfo Lanshore
  • 97
  • 1
  • 1
  • 11
  • 1
    Firebase Queries can only contain a single order/filter condition. Sometimes you can combine the values that you want to order/filter on into a single property. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Oct 27 '16 at 22:33

2 Answers2

1

Actually firebase only supports filtering/ordering with one propery, but if you want to filter with more than one property like you said I want to filter with age and name, you have to use composite keys.

There is a third party library called querybase which gives you some capabilities of multy property filtering. See https://github.com/davideast/Querybase

Vladimir Gabrielyan
  • 801
  • 1
  • 11
  • 23
0

You cannot query by multiple keys. If you need to sort by two properties your options are:

  1. Create a hybrid key. In reference to your example, if you wanted to get all 'Dino' and height '25' then you would create a hybrid name_age key which could look something like Dino_25. This will allow you to query and search for items with exactly the same value but you lose the ability for ordering (i.e. age less than x).
  2. Perform one query on Firebase and the other client side. You can query by name on Firebase and then iterate through the results and keep the results that match age 25.

Without knowing much about your schema I would advise you to make sure you're flattening your data sufficiently. Often I have found that many multi-level queries can be solved by looking at how I'm storing the data. This is not always the case and sometimes you may just have to take one of the routes I have mentioned above.

Nitish K.
  • 391
  • 2
  • 6