0
votes

db:

users

    ---key1
    ---key2
    ---key3
    ---key4



var array = [key1,key2,key4]

how do I write a query to retrieve key1, key2 and key4 and not key3 since it is not in array? I was doing this by pulling all keys and fetching it in client side.

1
Firebase doesn't currently have a way to do that on their side. You'll have to keep doing it in the client side. - Rosário Pereira Fernandes
There is no Firebase equivalent to SELECT * FROM users WHERE id IN (1,2,4), so you'll have to load each user with a separate call. This isn't nearly as slow as most developers think, because Firebase pipelines the requests: stackoverflow.com/questions/35931526/…. If you're having problems loading the users, update your question to show the minimal code that reproduces where you are stuck. - Frank van Puffelen
thanks i will stick to client side - newjsstu
Or, add a child node to key1, key 2 and key4 that adds commonality between them so you can then query for those specific nodes. i.e. there must be a reason that you want those particular keys; something they have in common. Whatever that is, add it as a child node and query for that common value. - Jay

1 Answers

0
votes

You have to manage it on client side using javascript indexOf function to check if a key exists in snapshot. There is no process you can manage it on server.

var array = [key1,key2,key4]
firebase.db().ref("users").once("value", function(snapshot){
    Object.keys(snapshot.val()).map(k => {
        if(array.indexOf(k)){
            console.log(snapshot.val())
        }
    })
})