我在我的应用程序中使用 Firestore 数据库。
现在,是否可以在多个字段中搜索特定单词?
例如:想象一个像“cheesecake”这样的词。现在,我收集了 100 个文档,每个文档有 10 个不同的字段。
我可以过滤 上的词吗?所有字段 ,以便它返回在 Flutter 中的任何字段中包含“cheesecake”一词的任何文档?
最佳答案
否 , 你不可以做这个。
看着Firebase Firestore Documentation page about queries ,您将了解以下内容:
Cloud Firestore does not support the following types of queries:
这并不直接适用于您的特定用例,但您的请求不起作用的原因是相似的:没有索引可以让您根据多个不同的字段进行排序。
你可以例如创建对每个字段进行排序的索引,但只能单独查询它们。要了解为什么不支持某些查询,您可以 check out this in depth explanatory video from the Firebase team .
最后,这意味着您将需要进行过滤客户端。
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
关于firebase - 我可以在 Firestore 搜索中过滤多个字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52277456/