![](/img/trans.png)
[英]In Firestore, how do I change for all documents of a collection of which a certain field has a certain value, that field in a certain value?
[英]How do i query a collection of firestore that has so many documents without an error in flutter?
我正在尝试从 firebase firestore 获取一些数据。
下面是我用来感染数据的代码
Future<List<Candidate>> findCandidateByName(String name) async {
final docs = await FirebaseFirestore.instance
.collection('candidates')
.where('name', isEqualTo: name)
.get()
.then((snapshot) => snapshot.docs);
return docs.map((doc) => Candidate.fromDoc(doc)).toList();
}
但是,每当我调用此函数时,应用程序都会在查询时崩溃。
以下是应用程序崩溃前的调试控制台消息
E/AndroidRuntime(25623): java.lang.OutOfMemoryError: Firestore (24.1.2) 内存不足。 检查您的查询以确保它们没有加载过多的数据。
我认为问题在于我发送查询的集合的大小。
“候选人”集合中有超过 40,000 份文档。
查询“候选人”集合会给我错误,但其他集合不会。
这个问题有什么解决办法吗?
您必须在函数中添加分页并在查询中使用限制,因为数据太多。
final docs = await FirebaseFirestore.instance
.collection('candidates')
.where('name', isEqualTo: name).limit(50)
.get();
如果您使用过滤器,那么在您的 Firestore 数据库中设置索引。 你可以从这里找到更多关于。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.