在两个条件之间查询Firebase数据库 [英] Query Firebase Database with two between conditions

查看:166
本文介绍了在两个条件之间查询Firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如果我的数据库看起来像这样这:

  MYAPP 
| _______________ items
| | _____ OshwYF72Jhd9bUw56W7d
| | |
| | | __item_name:plank_5
| | | __length:120
| | | __width:50
| |
| | _____ KbHy4293dYgVtT9pdoW
| | _____ PS8tgw53SnO892Jhweh
| | _____ Gicuwy8r23ndoijdakr
|
| ___ customers

我想查询数据库中的 item_name 的长度介于100-150之间,宽度介于30-70之间的项目,是否可以通过Firebase查询来实现?



我在这里阅读了答案:基于firebase中多个where子句的查询,但不涉及 / 范围子句之间的多个 ,这是我在上面的场景需要的。



我在另一个答案中看到了这个插件: https://github.com/davideast/Querybase ,但其中子句似乎没有采取一个范围值。例如:

$ $ p $ code $ const $ queriedDbRef = querybaseRef
。其中(
长度:(在100-150之间),
宽度(30-70之间)
});

这样的查询甚至可能吗?或者我将得到所有项目匹配一个条件,然后应用第二个条件客户端,使用Javascript?

解决方案

Firebase数据库查询只能对一个属性进行排序/过滤。这是一个硬性的限制,不可能在不久的将来改变。



一些情况下,可以将多个值合并为一个财产,这样我回答你链接或在QueryBase。但在这些情况下,你只能对这些值进行相对/范围查询:即最后一个。



唯一的情况是某人据我所知)曾经在GeoFire中实现过多值范围查询,在这里我们把一个位置的经度和纬度组合成一个所谓的 GeoHash 。我强烈推荐阅读更多关于geohashes的内容,因为它们做的事情非常独特:将两个数值合并成一个字符串值,允许对其数字组件进行排序和过滤。

从查看要组合的两个值(长度和宽度),可能可能从它们创建一个类似的组合字符串。但是要求你找到一种方法来为你的用例组合这两个值。我能想到的最简单的方法是存储区域,所以length * width:

  {
items:{
OshwYF72Jhd9bUw56W7d:{
item_name:plank_5,
length:120,//将长度和宽度存储为字符串,请修正
width:50,
area:6000
}
}
}

通过这种方式,您可以筛选3000(100x30)到10500(70x150)之间的物品:

  var query = ref.orderBy(area)。startAt(3000).endAt(10500); 

这个查询将匹配太多的项目,所以您将不得不执行一些额外的过滤客户端拒绝不匹配(Geofire做同样的btw):

$ $ p $ $ $ $ $ $ $ $ query.on(child_added,function(snapshot){$ b (item.length> = 100&& item.length< = 150 $ b&&& item.width> = 30; $ b var item = snapshot.val(); && item.width< = 70){
// TODO:对项目
}进行操作
});


I'm wondering if it's possible to return a list of values from my Firebase database where each element satisfies two conditions.

For example, if my database looked like this:

MYAPP
|_______________items
|                   |_____OshwYF72Jhd9bUw56W7d
|                   |                   |
|                   |                   |__item_name:"plank_5"
|                   |                   |__length:"120"            
|                   |                   |__width:"50"             
|                   |                            
|                   |_____KbHy4293dYgVtT9pdoW
|                   |_____PS8tgw53SnO892Jhweh
|                   |_____Gicuwy8r23ndoijdakr
|
|___customers

And I want to query the database for the item_name of every item that has a length of between 100-150 and a width of between 30-70, is there a way for me to do this with a Firebase query?

I've read the answers here: Query based on multiple where clauses in firebase but that doesn't deal with multiple between/range clauses, which is what I would need in the above scenario.

I saw this plugin in another answer: https://github.com/davideast/Querybase but the where clause doesn't seem to take a range of values. For example:

const queriedDbRef = querybaseRef
  .where({
    length: (between 100-150),
    width: (between 30-70)
  });

Is such a query even possible? Or will I have to get all items matching one condition, and then apply the second condition client-side, using Javascript?

解决方案

A Firebase Database query can only order/filter on one property. That's a hard limit that is not likely to change in the near future.

In some cases it is possible to combine multiple values into a single property, such my answer you linked or in QueryBase. But in those cases as you found, you can only perform a relative/range query on a single of those values: i.e. the last one.

The only case where someone (to my knowing) has ever implemented multi-value range querying is in GeoFire, where we combine the longitude and latitude of a location into a so-called GeoHash. I highly recommend reading more about geohashes, because they do something quite unique: combine two numerical values, into a single string value that allows ordering and filtering on its numeric components.

From looking at the two values you want to combine (length and width), it might be possible to create a similar composite string from them. But requires that you find a way to combine the two values for your use-case. The simplest possible way I can think if would be to store the area, so length * width:

{
  items: {
    "OshwYF72Jhd9bUw56W7d": {
      item_name:"plank_5",
      length: 120, // you store length and width as strings, please fix that
      width: 50,
      area: 6000
    }
  }
}

That way you can filter for items with an area between 3000 (100x30) and 10500 (70x150):

var query = ref.orderBy("area").startAt(3000).endAt(10500);

This query will match too many items, so you will have to perform some extra filtering client-side to reject the mismatches (Geofire does the same btw):

query.on("child_added", function(snapshot) {
  var item = snapshot.val();
  if (item.length >= 100 && item.length <= 150
      && item.width >= 30 && item.width <= 70) {
    // TODO: do something with the item
  }
});

这篇关于在两个条件之间查询Firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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