gpt4 book ai didi

How do I query a Firebase collection using WHERE 'not in' an array with more than 10 values?(如何使用包含10个以上值的数组中的Where‘Not in’来查询Firebase集合?)

转载 作者:bug小助手 更新时间:2023-10-25 18:21:09 26 4
gpt4 key购买 nike



In other posts, the solution presented for the 10 value limit was to loop and request in batches of 10, as explained here. This works fine if the operation is "in", as you can simply remove duplicates in the end.

在其他帖子中,针对10的值限制提出的解决方案是以10为一批进行循环和请求,如下所述。如果操作是“in”,则可以很好地工作,因为最后只需删除重复项即可。


However, it's absolutely not the case with the "not in" operation.

然而,“不在”行动绝对不是这样的。


Example: Let's say I have documents with ids from 1 to 21 in a certain collection. (for this example we'll disregard the existence of the ">" or "<" operators).

例如,假设我在某个集合中有ID从1到21的文档。(对于本例,我们将忽略“>”或“<”运算符的存在)。


We have an array of ids we want to ignore in our query, ranging from 0 to 20, and we want to retrieve only one document that escapes that range.

我们在查询中有一个要忽略的ID数组,范围从0到20,并且我们只想检索一个超出该范围的文档。


Normally, we would use the "not in" operation and limit our query to 1 document. However, there's a limit of 10 values for the not in array, so we're in a huge pickle.

通常,我们会使用“not in”操作,并将查询限制为1个文档。然而,对于not in数组,有10个值的限制,所以我们陷入了一个巨大的困境。



  1. We'll have to split the array in half, one ranging from 1 to 10 and the other from 11 to 20.



  2. We can no longer limit our query to 1 document, as the chances of us getting the the same value in both queries are slim.



  3. We'll end up having to make 2 queries, retrieve 11 documents in the first one, and 11 documents in the second one. And we'll find the intersection: 21.




So, bottom line, we wasted 22 reads to get 1 document. And the number of reads required to find this intersection will grow exponentially as we have more documents and our blacklist array grows.

所以,归根结底,我们浪费了22次阅读才得到了1个文档。随着我们拥有更多的文档和我们的黑名单阵列,查找此交叉点所需的读取次数将呈指数级增长。


So, my question is: Is there really no way around this? Because this is not a solution, it's a (bad) workaround. This honestly feels like a humongous flaw in Firestore, and I'm honestly not sure what I'll do to fix my problem at this point.

所以,我的问题是:真的没有办法绕过这一点吗?因为这不是一个解决方案,而是一个(糟糕的)变通办法。老实说,这感觉就像FiRestore中的一个巨大缺陷,而且我真的不确定在这一点上我该做些什么来解决我的问题。


更多回答
优秀答案推荐


For not-in clauses with more than the maximum number of supported values, I'd do something similar (but not the same) as you do for in. Start by querying with the maximum number of values, and then post-filter the documents in your application code to exclude the extraneous values.

对于超过支持的最大值数的NOT-In子句,我会做一些与您对In所做的类似的事情(但不是相同的)。首先查询最大数量的值,然后对应用程序代码中的文档进行后过滤,以排除无关的值。


Note by the way that I think the limit may have been increased to 30 values, as part of the changes when Firestore added support for OR clauses. The documentation still mentions the limit as 10, but give it a try and let me know if you can pass more values (or not).

顺便说一句,我认为限制可能已经增加到30个值,这是FiRestore添加对OR子句支持时更改的一部分。文档仍然提到限制为10,但试一试,让我知道您是否可以传递更多的值(或不能)。


更多回答

Hey Frank, thanks for the feedback. I've really given it some thought, and even if the limit was 30, it wouldn't do me any good, as there would always be a chance the limit could be surpassed. Querying all the values is also not an option, as this type of query will be often used by my users, and my costs would go through the roof. My workaround will be to have a single document with an array field containing all my document ids. That way I'll be able to query all the ids without wasting too many reads, filter after retrieving them and make a single get request afterwards.

嗨,弗兰克,谢谢你的反馈。我真的考虑过了,即使上限是30,也不会对我有任何好处,因为总是有可能超过上限。查询所有值也不是一种选择,因为这种类型的查询将经常被我的用户使用,并且我的成本将会飞涨。我的解决办法是只有一个文档,其中包含一个包含所有文档ID的数组字段。这样,我将能够在不浪费太多读取的情况下查询所有ID,在检索到它们之后进行过滤,然后发出单个GET请求。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com