How to implement "not in" in firebase?

Member

by orpha , in category: MySQL , a month ago

How to implement "not in" in firebase?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , a month ago

@orpha 

In Firebase, you can implement the "not in" functionality by using logical operators such as "!=" or "!==". You can also use a combination of "!=," "&&," and "!" to achieve the "not in" functionality.


For example, if you have a database structure like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "users": {
    "user1": {
      "name": "Alice"
    },
    "user2": {
      "name": "Bob"
    },
    "user3": {
      "name": "Charlie"
    }
  }
}


and you want to query for users whose names are not "Alice" and "Bob," you can use the following code:

1
2
3
4
5
6
7
8
firebase.database().ref('users').orderByChild('name').startAt('').endAt('').once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var name = childSnapshot.val().name;
    if(name != 'Alice' && name != 'Bob') {
      console.log(name);
    }
  });
});


This code snippet will retrieve all users whose names are not "Alice" and "Bob". You can modify the condition inside the if statement to fit your specific requirements.