0

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.

newjsstu
  • 11
  • 3
  • 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 Feb 25 '18 at 13:16
  • 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: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786. If you're having problems loading the users, update your question to show the [minimal code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Feb 25 '18 at 15:59
  • thanks i will stick to client side – newjsstu Feb 25 '18 at 16:03
  • 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 Feb 28 '18 at 23:23

1 Answers1

0

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())
        }
    })
})
Himanshu
  • 12,071
  • 7
  • 46
  • 61