Firestore:如何执行不等于/不等于的查询 [英] Firestore: how to perform a query with inequality / not equals

查看:50
本文介绍了Firestore:如何执行不等于/不等于的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Firestore 集合中选择不是我写的文章.
真的有那么难吗?

I want select from Firestore collection just articles written NOT by me.
Is it really so hard?

每篇文章都有字段owner_uid".

Every article has field "owner_uid".

就是这样:
我只是想编写等效于 select * from articles where uid<>request.auth.uid"

TL;DR:已找到解决方案: 语言/平台的用法:https://firebase.google.com/docs/firestore/query-data/queries#kotlin+ktx_5

推荐答案

EDIT Sep 18 2020

Firebase 发行说明表明现在有not-in!= 查询.(适当的文档现已可用.)

The Firebase release notes suggest there are now not-in and != queries. (Proper documentation is now available.)

  • not-in 查找指定字段的值不在指定数组中的文档.
  • != 查找指定字段值不等于指定值的文档.
  • not-in finds documents where a specified field’s value is not in a specified array.
  • != finds documents where a specified field's value does not equal the specified value.

查询运算符都不会匹配不存在指定字段的文档.请务必参阅文档了解您的语言的语法.

Neither query operator will match documents where the specified field is not present. Be sure the see the documentation for the syntax for your language.

原答案

Firestore 不提供不等式检查.根据文档:

Firestore doesn't provide inequality checks. According to the documentation:

where() 方法接受三个参数:要过滤的字段、比较操作和值.比较可以是 <、<=、==、> 或 >=.

The where() method takes three parameters: a field to filter on, a comparison operation, and a value. The comparison can be <, <=, ==, >, or >=.

不等式操作不像其他使用索引.Firestore 索引适用于范围查询.使用这种类型的索引,对于不等式查询,后端仍然需要扫描集合中的每个文档才能得出结果,当文档数量变大时,这对性能非常不利.

Inequality operations don't scale like other operations that use an index. Firestore indexes are good for range queries. With this type of index, for an inequality query, the backend would still have to scan every document in the collection in order to come up with results, and that's extremely bad for performance when the number of documents grows large.

如果您需要过滤结果以删除特定项目,您仍然可以在本地执行此操作.

If you need to filter your results to remove particular items, you can still do that locally.

您还可以选择使用多个查询来排除不同的值.像这样的东西,如果你想要除了 12 之外的所有东西.12、然后查询值>12、然后在客户端合并结果.

You also have the option of using multiple queries to exclude a distinct value. Something like this, if you want everything except 12. Query for value < 12, then query for value > 12, then merge the results in the client.

这篇关于Firestore:如何执行不等于/不等于的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆