0

I have something like this:

"records" : {
    "-KOHf6O3V35jqR4mENo1" : {
        "name" : "Foo",
        "type" : "One"
    },
    "-KOHf6O3V35jqR4mENo2" : {
        "name" : "Bar",
        "type" : "Two"
    },
    "-KOHf6O3V35jqR4mENo3" : {
        "name" : "Qux",
        "type" : "One"
    },
    "-KOHf6O3V35jqR4mENo4" : {
        "name" : "Baz",
        "type" : "One"
    },
}

I want to get the first two records with type == "One" ordered by name.

I can get the first two records ordered by name using:

var ref = firebase.database().ref("records");
ref.orderByChild("name").limitToFirst(2).on("child_added", function(snapshot) {
  console.log(snapshot.val());
});

I also can get all the records with type == "One":

var ref = firebase.database().ref("records");
ref.orderByChild("type").equalTo("One").on("child_added", function(snapshot) {
  console.log(snapshot.val());
});

Is there any way to order the records by a property and filter them by another?
(The real record list would have thousands of records)

pomber
  • 23,132
  • 10
  • 81
  • 94
  • You can only have one `orderBy...()` call in a Firebase query. Sometimes you can get the result you want by having a single property that combines the values you want to order/filter on. See http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase – Frank van Puffelen Sep 15 '16 at 03:19
  • @frank I think this is a different question/answer. Having a combined property may help for filtering by two properties (linked question), but not for sorting and filtering (this question). What parameter would you use in the equalTo function? – pomber Sep 15 '16 at 15:59
  • You'll need a combined property like `name_type: "Bar_One"` and then query `ref.orderBy("name_type").startAt("Bar_!").endAt("Bar_~")` or something along those lines. – Frank van Puffelen Sep 15 '16 at 17:42

0 Answers0