Hit*_*goi 5 dart firebase flutter google-cloud-firestore
我在我的应用程序中使用 Firestore 数据库。
现在,是否可以在多个字段中搜索特定单词?
例如:想象一个像“cheesecake”这样的词。现在,我收集了 100 个文档,每个文档有 10 个不同的字段。在 Flutter 中,我可以过滤所有字段中的
单词,以便返回任何字段中包含单词“cheesecake”的文档吗?
不,你不能这样做。
查看有关查询的 Firebase Firestore 文档页面,您将了解以下内容:
Cloud Firestore 不支持以下类型的查询:
这并不直接适用于您的特定用例,但您的请求不起作用的原因是类似的:没有索引允许您根据多个不同的字段进行排序。
例如,您可以创建对每个字段进行排序的索引,但仅单独查询它们。要了解为什么某些查询不受支持,您可以查看 Firebase 团队提供的深入说明视频。
总之,这意味着您将需要在客户端进行过滤。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14962 次 |
| 最近记录: |