Javascript 在Firebase中的多个子属性上筛选产品

Javascript 在Firebase中的多个子属性上筛选产品,javascript,angularjs,firebase,nosql,Javascript,Angularjs,Firebase,Nosql,我最近问过如何根据产品的子属性筛选产品(请参见:) 作为总结,我的结构如下所示: products/ product1 /author: 12345 /title: "Awesome" /category: "catA" /description: "more awesome" product2 /author: 67890 /title: "Other" /cat

我最近问过如何根据产品的子属性筛选产品(请参见:)

作为总结,我的结构如下所示:

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"
要筛选出所有使用author
12345
的产品,我们只需使用:

ref.child("products").orderByChild('author').equalTo(12345)...
但是,当我有多个AND语句或多个or语句时,该怎么办?

那么,如果我想过滤掉:

  • 具有作者
    12345
    和类别
    catA
    的所有产品(返回
    product1

  • 所有带有author
    12345
    或author 67890的产品(返回
    product1
    product3

第一部分,我尝试:

ref.child("products").orderByChild('author').equalTo(12345).orderByChild('category').equalTo('catA')...
但这不起作用(抛出错误
您不能合并多个orderBy调用

另见:

根据我所读的内容,实现这一点的方法是执行第一个查询(最好是返回最少结果的查询),然后通过javascript中的另一个字段进行过滤。另一种方法是为查询创建一个复合索引,如:;Frank van Puffelen在上面的链接中给出了一个例子

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"

至于“或”,我已经找了好一阵子,没有找到任何可以帮助你的东西。我建议两个单独的问题。

非常感谢您的反馈。是的,我猜复合索引是最好的选择。看到了吗