在 Firebase 中过滤多个 child 属性的产品

Filter products on multiple child properties in Firebase

我最近询问如何根据 child 属性 筛选产品(参见:)。

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

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"

要过滤掉作者为 12345 的所有产品,我们可以简单地使用:

ref.child("products").orderByChild('author').equalTo(12345)...

但是,当我有多个AND语句或多个OR语句时怎么办?

那我要过滤掉怎么办:

对于第一部分,我试过:

ref.child("products").orderByChild('author').equalTo(12345).orderByChild('category').equalTo('catA')...

但这没有用(抛出错误 You can't combine multiple orderBy calls

另请参阅: https://www.firebase.com/docs/web/guide/retrieving-data.html#section-queries

据我所知 here,执行此操作的方法是执行第一个查询(最好是 return 结果最少的查询),然后按另一个字段过滤在 javascript。另一种方法是为查询创建一个复合索引,如果您经常执行此查询,我建议您这样做;上面 link 中 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"

至于'OR',我找了好久也没找到对你有帮助的东西。我建议两个单独的查询。