7

I have this list in Firebase

"games":{
          "-Kaaaaaaaaaaaaaaaas" : {
            "name" : "name_a"
          },
          "-Kbbbbbbbbbbbbbbbbs" : {
            "name" : "name_b"
          },
          "-Kccccccccccccccccs" : {
            "name" : "name_c"
          },
          "-Kdddddddddddddddds" : {
            "name" : "name_d"
          },
        }

and I want to query only two of the games. I have their keys:

"-Kaaaaaaaaaaaaaaaas"
"-Kbbbbbbbbbbbbbbbbs"

I want to do this with something like:

WHERE key="-Kaaaaaaaaaaaaaaaas" OR key="-Kbbbbbbbbbbbbbbbbs"
...
WHERE key IN ("-Kaaaaaaaaaaaaaaaas" , "-Kbbbbbbbbbbbbbbbbs")

Or using Firebase code:

var gamesRef = ref.child("games");
var query = gamesRef.orderByKey().equalTo("-Kaaaaaaaaaaaaaaaas" , "-Kbbbbbbbbbbbbbbbbs", ...);
var list = $firebaseArray(query);

This is not possible because equalTo can accept only:

equalTo(String value, String key)
equalTo(double value)
equalTo(double value, String key)
equalTo(String value)
equalTo(boolean value, String key)
equalTo(boolean value)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
makkasi
  • 6,328
  • 4
  • 45
  • 60
  • 5
    You can't. You can query using a single value or a range. Firebase will [pipeline](http://stackoverflow.com/a/35932786/6680611) queries, so making two queries is not as inefficient as you might think. – cartant Nov 04 '16 at 10:13
  • 2
    OK, but why there is not just inList() function? Firebase is NoSQL Google database. Datastore is as well NoSQL Google database. So why both are NoSQL databases and the first doesn't have what the second have (Datastore has IN filter)? Isn't it possible firebase to chain these multiple queries on the server and to combine the result for us? It doesn't sound impossible.____So if there is no solution I will keep my list small and will fetch all objects in it instead of just what I need. – makkasi Nov 04 '16 at 11:57
  • 4
    *firebaser here* Makkasi: we could indeed combine the queries on the server and return them as a single result. The benefit of that would be minimal (in most cases we've seen) though, the linked answer explains why. The difference between load multiple items with a loop of `once()` calls and a hypothetical `WHERE id IN (1,2,3)` is literally the overhead of some web socket frames. If the first one is too slow, you're loading too much data. A different API wouldn't fix that. – Frank van Puffelen Nov 04 '16 at 14:14
  • 1
    @FrankvanPuffelen I can see that performance-wise it may not be all that different to execute individual queries, but what about error handling or even simply knowing when you have retrieved all the data? If I have 10 objects to load by keys, I can use the same event listener to add each one to an ArrayList or whatever, but how do I confirm I've loaded all of them so I can move on to using the data? In the listener do i just check that the length of my ArrayList is the same as the number of keys I set out to load after adding the object to the list? (I'm working on Android btw) – RobertoCuba Jun 25 '17 at 21:27

0 Answers0