gpt4 book ai didi

firebase - 基于 Firebase 中的多个 where 子句的查询

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:09 24 4
gpt4 key购买 nike

{
"movies": {
"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson"
},
"movie2": {
"genre": "Horror",
"name": "The Shining",
"lead": "Jack Nicholson"
},
"movie3": {
"genre": "comedy",
"name": "The Mask",
"lead": "Jim Carrey"
}
}
}

我是 Firebase 新手。如何从上面的数据中检索结果 where genre = 'comedy' AND lead = 'Jack Nicholson'

我有哪些选择?

最佳答案

使用 Firebase's Query API ,您可能会想试试这个:

// !!! THIS WILL NOT WORK !!!
ref
.orderBy('genre')
.startAt('comedy').endAt('comedy')
.orderBy('lead') // !!! THIS LINE WILL RAISE AN ERROR !!!
.startAt('Jack Nicholson').endAt('Jack Nicholson')
.on('value', function(snapshot) {
console.log(snapshot.val());
});

但正如来自 Firebase 的@RobDiMarco 在评论中所说:

multiple orderBy() calls will throw an error

所以我上面的代码将不起作用

我知道三种可行的方法。

1。在服务器上过滤大部分,在客户端完成其余部分

可以做的是在服务器上执行一个orderBy().startAt()./endAt(),提取剩余数据并在JavaScript 中过滤客户端上的代码。

ref
.orderBy('genre')
.equalTo('comedy')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
if (movie.lead == 'Jack Nicholson') {
console.log(movie);
}
});

2。添加一个属性,该属性组合了您要过滤的值

如果这还不够好,您应该考虑修改/扩展您的数据以支持您的用例。例如:您可以将 genre+lead 填充到您仅用于此过滤器的单个属性中。

"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson",
"genre_lead": "comedy_Jack Nicholson"
}, //...

您实际上是在以这种方式构建自己的多列索引,并且可以通过以下方式查询它:

ref
.orderBy('genre_lead')
.equalTo('comedy_Jack Nicholson')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
console.log(movie);
});

David East 写了一篇 library called QueryBase that helps with generating such properties .

您甚至可以执行相对/范围查询,假设您希望允许按类别和年份查询电影。您将使用此数据结构:

"movie1": {
"genre": "comedy",
"name": "As good as it gets",
"lead": "Jack Nicholson",
"genre_year": "comedy_1997"
}, //...

然后使用以下命令查询 90 年代的喜剧:

ref
.orderBy('genre_year')
.startAt('comedy_1990')
.endAt('comedy_2000')
.on('child_added', function(snapshot) {
var movie = snapshot.val();
console.log(movie);
});

如果您需要过滤的不仅仅是年份,请确保按降序添加其他日期部分,例如“comedy_1997-12-25”。这样,Firebase 对字符串值的字典顺序将与时间顺序相同。

属性中值的这种组合可以处理两个以上的值,但您只能对复合属性中的最后一个值进行范围过滤。

一个非常特殊的变体是由 GeoFire library for Firebase 实现的.该库将位置的纬度和经度组合成所谓的 Geohash。 ,然后可用于在 Firebase 上进行实时范围查询。

3。以编程方式创建自定义索引

另一种选择是做我们在添加这个新的查询 API 之前所做的一切:在不同的节点中创建索引:

  "movies"
// the same structure you have today
"by_genre"
"comedy"
"by_lead"
"Jack Nicholson"
"movie1"
"Jim Carrey"
"movie3"
"Horror"
"by_lead"
"Jack Nicholson"
"movie2"

可能还有更多方法。例如,此答案突出显示了另一种树形自定义索引:https://stackoverflow.com/a/34105063


如果这些选项都不适合您,但您仍想将数据存储在 Firebase 中,您也可以考虑使用它的 Cloud Firestore database .

Cloud Firestore 可以在单个查询中处理多个相等性过滤器,但只能处理一个范围过滤器。在引擎盖下,它本质上使用相同的查询模型,但它就像是为您自动生成复合属性。请参阅关于 compound queries 的 Firestore 文档.

关于firebase - 基于 Firebase 中的多个 where 子句的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com