firebase - 我可以在 Firestore 搜索中过滤多个字段吗?

标签 firebase dart flutter google-cloud-firestore

我在我的应用程序中使用 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/

相关文章:

dart - 如何强制实现抽象类的类声明其变量?

sqlite - Flutter:在进行大量数据库操作时避免 UI 卡住

javascript - 在 Firebase 中实现 session cookie 后无法读取属性 'getIdToken'

android - NoClassDefFoundError : com. google.firebase.perf.internal.RemoteConfigManager

Firebase on() 不返回任何内容

Flutter Url Launcher 不会将整个文本传递给短信

Flutter 从 future<File> 创建字符串以检查文件名的最后 3 个字符

Firebase - 可以创建多少个数据库?

flutter - 将dart中的 map 更新为firestore

flutter - 如何使用 dart 和 Flutter 在 Android 中获取所有移动设备测量值