[英]How to make a complex query to the firebase in flutter
My firestore-firebase database looks like:我的firestore-firebase 数据库如下所示:
products -> collection Name
| uid1 -> Document id
| price : "200"
| category: "a"
| uid2 -> Document id
| price : "500"
| category: "b"
| uid3 -> Document id
| price : "400"
| category: "b"
Now i want to get products ranging from 200 to 400
and with category b
, I should get the product uid3
现在我想获得从
200 to 400
的产品,并且category b
,我应该获得产品uid3
Now the values which are required are taken from the filter screen ui, which has price range slider and then it even has 6-8 categories, My interpretation of trying was creating field for each of the category and then querying, But i dont think so this is a good approach,现在需要的值取自过滤器屏幕 ui,它有价格范围滑块,然后它甚至有 6-8 个类别,我对尝试的解释是为每个类别创建字段然后查询,但我不这么认为这是一个很好的方法,
Because i'll have to write search query for all permutation combination of the category toggle for example if categoryA is true , categoryB is false, categoryC is true,因为我必须为类别切换的所有排列组合编写搜索查询,例如如果 categoryA 为 true , categoryB 为 false , categoryC 为 true ,
Then i'll have to write for all the category's fetching from the firebase,然后我将不得不为从火力库中获取的所有类别写,
if categoryA is true
...
where('categoryA',isEqualto:"true").get();
if categoryB is true
...
where('categoryB',isEqualto:'false').get();
...
...
...
if categoryA is true categoryB is false categoryC is true
...
where('categoryA',isEqualto:"true")
.where('categoryC', isEqualto:"true).get();
and so on and so forth which is obviosly not recommended.等等等等,这显然是不推荐的。
how to overcome this approach and how to query such a requirement如何克服这种方法以及如何查询这样的要求
Example Scenario示例场景
variable i have is :我的变量是:
minPrice : String,
maxPrice : String,
categoryA : bool,
categoryB : bool
For the given example value of attributes are :对于给定的示例属性值是:
minPrice : "200",
maxPrice : "400",
categoryA : false,
categoryB : true,
I want a Future result , How to write such a query in firebase ?我想要一个未来的结果,如何在 firebase 中编写这样的查询?
And which fields should be indexed in firebase for the effective search ?哪些字段应该在 firebase 中被索引以进行有效搜索?
Thank you in advance !!先感谢您 !!
Edit :编辑:
This is the fucntion which i tried just for the price, Don't know how to do for the category.这是我为价格尝试的功能,不知道如何为类别做。
Future<List<ProductModel>> getFilteredProducts(String maxPrice, String minPrice) => FirebaseFirestore.instance
.collection("products")
.where("price", isLessThanOrEqualTo: maxPrice)
.where("price", isGreaterThanOrEqualTo: minPrice)
.get()
.then((value) => value.docs.map((e) => ProductModel.fromMap(e.data(), e.id)).toList());
Check here for Information about where
query 在此处查看有关
where
查询的信息
Note: this is just a sample注意:这只是一个示例
await FirebaseFirestore.collection('products'),
.where('price', greaterThanorEqualto: searchText)
.where(categoryB, Equalto: true)
.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.