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

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

问题描述

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

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

解决方案

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

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

这并不直接适用于您的特定用例,但是您的请求无法正常工作的原因是相似的:没有索引可让您根据多个不同的字段进行排序.
您可以例如创建对每个字段进行排序的索引,但只能单独查询它们.要了解为什么不支持某些查询的原因,您可以查看Firebase团队的深入解释性视频./p>

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

Flutter中的解决方法概念

  final QuerySnapshot querySnapshot =等待Firestore.instance.collection('cakes').getDocuments();最终List< DocumentSnapshot>文件=querySnapshot.documents.where(((snapshot)=> snapshot.data.containsValue('cheesecake')));//现在[documents]对象将只包含带有"cheesecake"的文档,作为他们的任何领域 

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

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?

解决方案

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

Cloud Firestore does not support the following types of queries:

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.

Workaround Concept in Flutter

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天全站免登陆