简体   繁体   English

使用 Flutter 在一次往返中从 firestore 获取超过 10 个文档

[英]get more than 10 documents from firestore in one round trip using Flutter

I am currently using whereIn for this.我目前正在为此使用whereIn But only get 10 or less douments at once.但一次只能获得 10 个或更少的文件。 whereIn 10 entries or less according to the firestore documentation根据 firestore 文档,whereIn 10 个条目或更少

Is there any way to get more than 10 documents at once in one round trip?有没有办法在一次往返中一次获得超过 10 个文件?

 return _db
        .collection('books')
        .where('docId', whereIn: ['id1', 'id2'...'id10'])
        .get()
        .then((value) => value.docs.map((e) => BookModel.fromFireStore(e)).toList());

No, 10 is a hard limit and can't be exceeded.不,10 是硬限制,不能超过。 You will need to perform multiple queries to get more than 10 documents by ID.您需要执行多个查询才能按 ID 获取 10 个以上的文档。 The documentation states:文件指出:

Use the in operator to combine up to 10 equality (==) clauses on the same field with a logical OR.使用in运算符将同一字段上的最多 10 个相等 (==) 子句与逻辑 OR 组合在一起。

The question has been answered.问题已得到解答。 Here I just want to show how you can get those documents with multiple round-trips cause it is not an easy task as well.在这里,我只想展示如何通过多次往返获取这些文档,因为这也不是一件容易的事。 It took me some time to implement this, so I hope it will save someone else's time.我花了一些时间来实现这个,所以我希望它能节省别人的时间。

First create a function for one round-trip:首先为一次往返创建一个 function:

Future<List<Book>> _getBooksByIds(ids) {
  return _instance
      .collection('books')
      .where(FieldPath.documentId, whereIn: ids)
      .get()
      .then((QuerySnapshot snapshot) {
    return snapshot.docs.map((DocumentSnapshot doc) => BookModel.fromSnapshot(doc)).toList();
  });
}

Then split by 10 your ids然后除以 10 你的 id

ids = ['id1', 'id2'...'id11'];
final idsBy10 = [
  for (var i = 0; i < ids.length; i += 10)
    ids.sublist(i, min(i + 10, ids.length))
];

Then run multiple requests and merge their results:然后运行多个请求并合并它们的结果:

Future.wait<List<Book>>(idsBy10.map((ids) => _getBooksByIds(ids)))
        .then((listOfLists) => listOfLists.expand((l) => l).toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Firestore - 如何在一次往返中通过多个 id 获取多个文档? - Google Firestore - How to get several documents by multiple ids in one round-trip? 无法从 Cloud firestore 中检索 Flutter 中的多个集合 - Cant Retrieve more than one collection in Flutter from cloud firestore 从 firestore 获取最后 10 个文件 - Get the 10 last documents from firestore Flutter &amp; Firebase 获取超过 10 个 Firebase 文档到 ZEAE835E83C0494A37392329 <list<map> &gt; </list<map> - Flutter & Firebase Get more than 10 Firebase Documents into a Stream<List<Map>> 无法从 flutter 上的 Firestore 获取文档 - Can't get documents from firestore on flutter Flutter 来自多个集合的 Firestore 搜索 - Flutter Firestore search from more than 1 collection 从 Cloud Function 将 500 多个文档上传到 Firestore 数据库 - Upload more than 500 documents to Firestore database from Cloud Function 如何在 Firestore -Flutter 中将所有文档从一个集合复制到另一个集合? - How to copy all documents from one collection to other in Firestore -Flutter? 如何使用 Flutter 和 Firestore 获取属于特定子集合的所有文档 - How to get all documents belong to specific subcollection using Flutter and Firestore 在多个文档上进行Firestore交易是否比逐个更新每个文档更有效? - Are Firestore transactions on multiple documents more efficient than updating each document one by one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM