0

I have a an array of ids which are generated using the firebase.ref().push() method. For example -

myarray = 
[-KoDeiUiqsMKDuXuwhzi, 
-KoDrz6ngJtNrr51tguD,
-KoDyC-_ZeSIAN1_oFk4]

I have a node of items some of which match these ids -

/mynode
  --KoDeiUiqsMKDuXuwhzi 
     -itemprop1 : "abc"
     -itemprop2 : "xyz"
     -id : "KoDeiUiqsMKDuXuwhzi"
  --KnurYH554YNW6bcWch5
     -itemprop1 : "opq"
     -itemprop2 : "zyc"
     -id : "KnurYH554YNW6bcWch5"
  --KoDrz6ngJtNrr51tguD
     -itemprop1 : "mon"
     -itemprop2 : "ooo"
     -id : "KoDrz6ngJtNrr51tguD"
          ..
          ..
          ..
          ..

..and so on.. basically the array of id's is a subset of this node. I want to retreive a all objects from this node where the id matches in the array. Currently the way i am doing is like this -

firebase.database().ref("mynode").once('value').then(function(snap){
   snap.forEach(function(childsnap){
     if(myarray.indexOf(childsnap.key) != -1){
        //add to result
      }
   })
})

Although this code works but i think i can use orderByChild, startAt and endAt filtering to get the list of objects. Is there a way to get the result by filtering instead of getting all data and then filtering out? Will it be more efficient than this code?

Nikhil
  • 1,166
  • 2
  • 17
  • 35
  • 2
    If you already know the ids you're looking for, you could also simply query them each directly at the their known paths rather than pulling down everything. – Doug Stevenson Jul 06 '17 at 17:04
  • yes i realise that but wouldnt that mean i have to query for every item in a loop? – Nikhil Jul 06 '17 at 17:34
  • I don't think that would be the worst thing, especially if you would otherwise be pulling down a lot of data you don't need. – Doug Stevenson Jul 06 '17 at 17:42
  • Looping reads are not as slow as you may think, since they are pipelined over the same socket connection. See http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Jul 06 '17 at 17:52

0 Answers0