firebase相当于sql在哪里in() [英] firebase equivalent to sql where in ()

查看:147
本文介绍了firebase相当于sql在哪里in()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

您如何在Firebase中执行此逻辑?



thx, 解决方案

Firebase的查询没有 / code>或 IN 运算符。因此,您不能轻易将上述查询映射到Firebase。

一个解决方法是实现一个简单地遍历元素的函数:

 函数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());
});
}
回调(行);





$ b注意上面的代码可能是递归的,所以用它来获取灵感而不是作为复制/粘贴代码。

另一个(也许更好的方法)解决这个问题是通过找出为什么选择这三行,为什么不选择其他。这些行可能有一些共同点。一旦你知道它们有什么共同之处,你可以把它建模成数据结构。

更新(20160408):对于一个很好的解释为什么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,

解决方案

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.

这篇关于firebase相当于sql在哪里in()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆