最近,我问怎么根据他们的子属性(见过滤产品:Filter根据火力地堡子子)的产品。
作为一个回顾我的结构如下:
产品/ 产品1 /作者:12345 /标题:真棒 /类别:CATA /描述:真棒多 产品2 /作者:67890 /标题:其他 /类别:CATB /描述:otherawesome 产品3 /作者:12345 /标题:比利 /类别:CATB /描述:otherawesome和筛选出与作者所有产品 12345 我们可以简单地使用:
ref.child(产品)。orderByChild('作家')。equalTo(12345)...但是,做什么时,我有多个AND陈述或者多个或声明?
所以,如果我想筛选出:
-
所有产品均拥有作者 12345 和类别 CATA (返回产品1 )
-
所有产品均拥有作者 12345 或作者67890(返回产品1 和产品3 )
有关第一部分,我想:
ref.child(\"products\").orderByChild('author').equalTo(12345).orderByChild('category').equalTo('catA')...但没有工作(会抛出错误不能将多个排序依据通话)
另请参阅: www.firebase/docs/web /guide/retrieving-data.html#section-queries
解决方案从我读过here,要做到这一点的方法是执行第一个查询(preferably一个将返回结果最少的),那么在JavaScript中的其他字段进行筛选。另一种方式来做到这一点,我建议这个,如果你经常执行该查询,是创建查询像一个复合索引;在上面的链接,答案由弗兰克面包车Puffelen给出了这样一个例子。
产品/ 产品1 /作者:12345 /标题:真棒 /类别:CATA /描述:真棒多 / author_category:12345_catA 产品2 /作者:67890 /标题:其他 /类别:CATB /描述:otherawesome / author_category:67890_catB 产品3 /作者:12345 /标题:比利 /类别:CATB /描述:otherawesome / author_category:12345_catB至于'OR',我已经看了好一阵子,发现无能为力了。我建议两个单独的查询。
I recently asked how to filter products based on their child property (see: Filter products based on the sub child in Firebase).
As a recap my structure looks as follows:
products/ product1 /author: 12345 /title: "Awesome" /category: "catA" /description: "more awesome" product2 /author: 67890 /title: "Other" /category: "catB" /description: "otherawesome" product3 /author: 12345 /title: "Billy" /category: "catB" /description: "otherawesome"And to filter out all products with author 12345 we can simply use:
ref.child("products").orderByChild('author').equalTo(12345)...However, what to do when I have multiple AND statements or multiple OR statements?
So what if I want to filter out:
all products with author 12345 AND category catA (returns product1)
all products with author 12345 OR author 67890 (returns product1 and product3)
For the first part, I tried:
ref.child("products").orderByChild('author').equalTo(12345).orderByChild('category').equalTo('catA')...but that did not work (throws error You can't combine multiple orderBy calls)
See also: www.firebase/docs/web/guide/retrieving-data.html#section-queries
解决方案From what I've read here, the way to do this is to perform the first query (preferably the one that will return the fewest results), then filter by the other field in javascript. Another way to do this, and I'd recommend this if you are performing this query often, is create a compound index for the query like; the answer by Frank van Puffelen in the link above gives an example of this.
products/ product1 /author: 12345 /title: "Awesome" /category: "catA" /description: "more awesome" /author_category: "12345_catA" product2 /author: 67890 /title: "Other" /category: "catB" /description: "otherawesome" /author_category: "67890_catB" product3 /author: 12345 /title: "Billy" /category: "catB" /description: "otherawesome" /author_category: "12345_catB"As for the 'OR', I've looked for quite a while and found nothing to help you. I'd suggest two separate queries.
更多推荐
在火力地堡多个子属性过滤器产品
发布评论