1

I'm giving shot at firebase, converting an existing database to firebase. How do i select a list where the value of a key equals a specific value?

getAllUsers() {
    return this.fireBase.list(
        '/users',
        ref => ref
            .orderByChild('last_name')
            // active = true
    ).valueChanges();
}
2
  • Are you trying to order by last name and then filter for a specific value of active (I.e. true)? Commented Jan 18, 2019 at 18:25
  • yes that is exactly what i am trying to do Commented Jan 18, 2019 at 18:26

1 Answer 1

1

With the Firebase Realtime Database you cannot use orderBy() on one field and then use equalTo() on another field. And you cannot use two different orderBy().

This is detailed/explained in the docs, here (angularfire2 ) and here ("standard" Javascript SDK).

However you could use a composed value like active_lastname with a query like the following one:

  var database = firebase.database();
  database
    .ref('users')
    .orderByChild('composedValue')
    .startAt('true_')
    .endAt('true_\uf8ff')
    .once('value', function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
        var childKey = childSnapshot.key;
        var childData = childSnapshot.val();
        console.log(childKey);
        console.log(childData);
      });
    });

You composed values would be like:

true_Obama
true_Bush
false_Carter
....

So for angularFire2, based on your code it would be:

getAllUsers() {
    return this.fireBase.list(
        '/users',
        ref => ref
            .orderByChild('composedValue')
            .startAt('true')
            .endAt('true\uf8ff')
    ).valueChanges();
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. i think it's more important for me to filter by the value, so i will handle the sorting by name on the client side.
Just note that the second part of my initial answer was wrong... You cannot reach your goal by using a composed values starting by the last_name. But you can achieve it with a composed value starting by the active value, see my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.