问题描述
我想知道是否可以从我的 Firebase 数据库中返回一个值列表,其中每个元素都满足两个条件.
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
我想在数据库中查询长度在 100-150 之间、宽度在 30-70 之间的每个项目的 item_name
,有没有办法让我做到这一点使用 Firebase 查询?
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?
我在这里阅读了答案:基于多个查询firebase 中的子句,但不处理多个 between
/range
子句,这是我在上述场景中需要的.
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.
我在另一个答案中看到了这个插件:https://github.com/davideast/Querybase但是 where
子句似乎没有取值范围.例如:
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)
});
这样的查询甚至可能吗?或者我是否必须获得符合一个条件的所有项目,然后使用 Javascript 在客户端应用第二个条件?
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?
推荐答案
Firebase 数据库查询只能对一个属性进行排序/过滤.这是一个硬性限制,在不久的将来不太可能改变.
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.
在某些情况下,可以将多个值组合到一个属性中,例如您链接的我的答案或在 QueryBase 中.但是在您发现的那些情况下,您只能对这些值中的一个执行相对/范围查询:即最后一个.
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.
某人(据我所知)实现多值范围查询的唯一情况是在 GeoFire 中,我们将位置的经度和纬度组合成所谓的 GeoHash.我强烈建议阅读有关 geohashes 的更多信息,因为它们做了一些非常独特的事情:将两个数值组合成一个字符串值,允许对其数值组件进行排序和过滤.
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.
通过查看您想要组合的两个值(长度和宽度),可能可以从它们创建一个类似的复合字符串.但是需要您找到一种方法来为您的用例组合这两个值.我能想到的最简单的方法是存储 area,所以长度 * 宽度:
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
}
}
}
这样您就可以过滤面积在 3000 (100x30) 和 10500 (70x150) 之间的项目:
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);
此查询将匹配太多项目,因此您必须在客户端执行一些额外的过滤以拒绝不匹配项(顺便说一句,Geofire 也是如此):
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 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!