我可以在 Firestore 搜索中过滤多个字段吗? [英] Can I filter Multiple Fields in a Firestore search?

查看:21
本文介绍了我可以在 Firestore 搜索中过滤多个字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用 Firestore 数据库.
现在,是否可以在多个字段中搜索特定单词?

I use Firestore database in my app.
Now, is it possible to search for a particular word in multiple fields?

例如:想象一个像cheesecake"这样的词.现在,我收集了 100 个文档,每个文档有 10 个不同的字段.
我是否可以过滤所有字段上的词,以便它返回中包含词cheesecake"的任何文档Flutter 中的任何字段?

For example: Imagine a word like "cheesecake". Now, I have 100 documents in collection and each document has 10 different fields.
Can I filter for the word on all of the fields, so that it returns any document that contains the word "cheesecake" in any of the fields, in Flutter?

推荐答案

,你不能这样做.
查看 有关查询的 Firebase Firestore 文档页面,您会发现关于以下内容:

No, you cannot do this.
Looking at the Firebase Firestore Documentation page about queries, you will find out about the following:

Cloud Firestore 不支持以下类型的查询:

Cloud Firestore does not support the following types of queries:

这并不直接适用于您的特定用例,但是您的请求不起作用的原因是类似的:没有索引可以让您根据多个不同的字段进行排序.
你可以例如创建对每个字段进行排序的索引,但只能单独查询它们.要了解某些查询不受支持的原因,您可以观看 Firebase 团队提供的此深入解释视频.

This does not directly apply to your specific use case, but the reasoning for why your request will not work is similar: There is no index that allows you to sort according to multiple different fields.
You could e.g. create indexes that sort each of the fields, but only query them individually. To understand why certain queries are not supported, you can check out this in depth explanatory video from the Firebase team.

最后,这意味着您需要在客户端进行过滤.

Concluding, this means that you will need to do the filtering client side.

final QuerySnapshot querySnapshot = 
  await Firestore.instance.collection('cakes').getDocuments();
final List<DocumentSnapshot> documents = 
  querySnapshot.documents.where((snapshot) => snapshot.data.containsValue('cheesecake'));
// now the [documents] object will only contain documents, which have "cheesecake" as any of their fields

这篇关于我可以在 Firestore 搜索中过滤多个字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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