Skip to content Skip to sidebar Skip to footer

How To Query Firebase For Value Between Two Keys

How can i search firebase data with a value, i want to get the data if the value comes in between the two keys Example If user enter any number between 1 to 5 return the below

Solution 1:

You can only query on one child so either start or end child.

What you can do is this:

ref.orderByChild("start").equalTo("4").on(...){...};

The above will give you the values when start=4

ref.orderByChild("start").limitToFirst(5).on(...){..};

this will give you the first 5 of child start

ref.orderByChild("start").startAt("1").endAt("5").on(..){..};

Solution 2:

As @Peter Haddad said "firebase only allows to query on one field" so what you can do to solve your issue is:

firebase.database().ref('classdata')
  .orderByChild('start')
  .startAt(1)
  .once('value', function (snapshot) {
    snapshot.forEach(function(childSnapshot) {
      var childKey = childSnapshot.key;
      var childData = childSnapshot.val();

      // Here you filter by your second fieldif (childData.end === '' || childData.end <= 5) {
        ...
      }
    });
  })

You can find here more documentation about how to filter data in firebase.


Here you can find some really good approaches to resolve your issue.

Post a Comment for "How To Query Firebase For Value Between Two Keys"