简体   繁体   English

Firebase 使用数组包含和范围过滤器进行查询 JavaScript

[英]Firebase Query using array-contains and a range filter JavaScript

I am trying to query documents that have a specific genre and user rating.我正在尝试查询具有特定类型和用户评分的文档。

In the document, genres is an array[] and userRating is a number out of 5.在文档中,genres 是一个数组 [],而 userRating 是 5 中的一个数字。

Here's my query:这是我的查询:

q = query(
            ColRef,
            where("genres", "array-contains", "Comedy"),
            where("userRating", ">=", 3)   
          );

I get this error when it's run运行时出现此错误

FirebaseError: [code=failed-precondition]

Firebase Documentation says Firebase 文档说

You can perform range (<, <=, >, >=) or not equals (,=) comparisons only on a single field: and you can include at most one array-contains or array-contains-any clause in a compound query:您只能对单个字段执行范围(<、<=、>、>=)或不等于(、=)比较:并且您最多可以在复合查询中包含一个 array-contains 或 array-contains-any 子句:

Am using one array-contains and one not equals or am I not understanding what they meant is array-contains also considered a range filter?正在使用一个数组包含和一个不等于,或者我不理解它们的意思是数组包含也被视为范围过滤器?

As mentioned earlier, you need a composite index to support this query:如前所述,您需要一个复合索引来支持此查询:

[genres - Arrays, userRating - Ascending]

By default, the query will order results in ascending order based on value of userRating .默认情况下,查询将根据userRating的值升序排列结果。 But your index mode was Descending for userRating and hence the first index did not work.但是您的索引模式是Descending for userRating ,因此第一个索引不起作用。

That index would have supported your query if you had specified orderBy('userRating', 'desc') which orders the results in descending order .如果您指定orderBy('userRating', 'desc')降序排列结果,该索引将支持您的查询。

You can read more about this in the documentation .您可以在文档中阅读有关此内容的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM