Firestore:多个条件where子句 [英] Firestore: Multiple conditional where clauses
问题描述
例如,我的书籍列表中有动态过滤器,我可以设置特定的颜色,作者和类别。
此过滤器可以同时设置多种颜色和多个类别。
For example I have dynamic filter for my list of books where I can set specific color, authors and categories. This filter can set multiple colors at once and multiple categories.
Book > Red, Blue > Adventure, Detective.
如何有条件地添加where?
How can I add "where" conditionally?
firebase
.firestore()
.collection("book")
.where("category", "==", )
.where("color", "==", )
.where("author", "==", )
.orderBy("date")
.get()
.then(querySnapshot => {...
推荐答案
正如您在API文档中看到的那样, collection()方法返回 CollectionReference 。CollectionReference扩展了查询 。 Query.where()和 Query.orderBy()也会返回Query对象。所以你可以像这样重写你的代码:
As you can see in the API docs, the collection() method returns a CollectionReference. CollectionReference extends Query. Query.where() and Query.orderBy() also return Query objects. So you can rewrite your code like this:
var query = firebase.firestore().collection("book")
query = query.where(...)
query = query.where(...)
query = query.where(...)
query = query.orderBy(...)
query.get().then(...)
现在你可以放入条件来确定你想在每个阶段应用哪些过滤器。
Now you can put in conditionals to figure out which filters you want to apply at each stage.
这篇关于Firestore:多个条件where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!