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

2023-12-04

我最近询问如何根据产品的子属性过滤产品(请参阅:根据 Firebase 中的子项过滤产品).

回顾一下我的结构如下:

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 语句时该怎么办?

那么如果我想过滤掉怎么办:

  • 与作者相关的所有产品12345与类catA(返回product1)

  • 与作者相关的所有产品12345或作者 67890(返回product1 and product3)

对于第一部分,我尝试了:

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/retrieve-data.html#section-queries


从我读到的here,执行此操作的方法是执行第一个查询(最好是返回最少结果的查询),然后按 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"

至于“OR”,我已经找了很长一段时间了,但没有找到任何可以帮助你的东西。我建议两个单独的查询。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Firebase 中过滤多个子属性的产品 的相关文章

随机推荐