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

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

问题描述

我需要一个与 SQL 中的此查询等效的 firebase 查询:

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

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

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

How would you do this logic in firebase?

谢谢,

推荐答案

Firebase 的查询没有 ORIN 运算符.因此,您无法轻松地将上述查询映射到 Firebase.

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.

更新 (20160408):有关为什么上述在 Firebase 上比您预期的要快得多的一个很好的解释,请参阅 这个答案.

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 where in ()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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