Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase equivalent to sql where in ()

Tags:

firebase

I need a firebase query that is equivalent to this query in SQL:

select * from your_table where id in (123, 345, 679)

How would you do this logic in firebase?

thx,

like image 636
kevinius Avatar asked Apr 10 '15 11:04

kevinius


1 Answers

Firebase's queries don't have an OR or IN operator. So you cannot easily map the above query to Firebase.

One workaround would be to implement a function that simply loops over the elements:

function loadThese(ids, callback) {
  var rows = [];
  while (ids.length > 0) {
    var id = ids.splice(0,1)[0];
    refToTable.child(id).once('value', function(snapshot) {
      rows.push(snapshot.val());
    });
  }
  callback(rows);
}

Note that the above should probably be recursive, so use it for inspiration and not as copy/paste code.

Another (and probably better way) to solve the problem is by figuring out why these three rows are selected and why not others. These rows probably have something in common. Once you know what they have in common, you can model that into the data structure.

Update (20160408): for a good explanation of why the above is a lot faster on Firebase than you might expect, see this answer.

like image 75
Frank van Puffelen Avatar answered Oct 09 '22 01:10

Frank van Puffelen