检索文档列表,仅限于数组中的ID-进行后续操作 [英] Retrieve a list of documents, limited to IDs in an array -- follow up

查看:81
本文介绍了检索文档列表,仅限于数组中的ID-进行后续操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对

This is a follow-up question to the question asked here. Long story short, I'm trying to limit a Flutter Firebase query only to values contained in a list.

这是工作代码,最后还有一个附加问题.使用份额"集合并将ID字段添加到项目"集合中,如下所示:

Here's the working code with an additional question at the end. Using the 'shares' collection and adding an ID field to the 'projects' collection as below:

我现在能够使用一个Stream来(按用户)检索我的股票,然后将该列表用作第二个Stream中的查询,如下所示:

I'm now able to use one Stream to retrieve my shares (per user), and then use that list as a query in a second Stream like so:

@override
Stream<Map<String, dynamic>> getUserSharesStream({@required String uid}) {
  return _firestore.collection('shares').doc(uid).snapshots().map((doc) => doc.data());
}

@override
Stream<List<Map>> getListOfProjectsForUser({@required String uid, @required List<String> shares}) {
  var ref = _firestore.collection('projects');
  return ref
      .where('id', whereIn: shares)
      .snapshots()
      .map((QuerySnapshot snapshot) => snapshot.docs.map((DocumentSnapshot doc) => doc.data()).toList());
}

在应用程序中,我得到与用户共享的项目列表,如下所示:

In the app, I get a list of the projects shared with my user like so:

stream: userSharesQuery,
builder: (BuildContext context, AsyncSnapshot snapshot) {
  if (snapshot.hasData) {
    Map<String, dynamic> result = snapshot.data;
    projectsList = result.keys.toList();

通过将该列表输入到我的projectsListStream.builder中,我得到了与我的用户共享的项目的列表,这是原始问题的答案.

By feeding that list into my projectsListStream.builder I get a list of the projects shared with my user, which was the answer to the original question.

我剩下的问题是:很明显,我在查询限制列表中只限于10个项目.那么 YOU 架构师将如何处理共享项目数更大大于10的实例?

My remaining question is: apparently I'm limited to 10 items in my query limit list. So how would YOU architect for instances where the number of shared projects is GREATER than 10?

我是否应该放弃尝试限制查询,而只是每次都解析整个项目列表,寻找与用户共享列表匹配的projectIds?还是有一种方法可以多次调用流,每次调用10条列表?还是您会建议一个完全不同的方案来完成任务?

Should I just abandon trying to limit my queries and simply parse the entire list of projects every time, looking for projectIds that match my user's share list? Or is there a way to call the stream more than once, each time with a list of 10? Or would you recommend an entirely different scheme for accomplishing the task?

推荐答案

我知道我来晚了一些,但是我试图提出类似的建议,所以如果有人想使用快照而不是拥有快照,这可能会有所帮助实时流

I know I'm a bit late but I was trying to come up with something similar so this can help if someone wants to use snapshot instead of get to have a realtime stream

使用 rx包 CombineLatestStream.list,您可以将多个流拆分成长度为10或10的数组少然后结合起来

using rx package CombineLatestStream.list you can have multiple streams splitted with an array of lenght 10 or less and then combine them

@override
Stream<List<Map>> getListOfProjectsForUser({@required String uid, @required List<String> shares}) {
  var ref = _firestore.collection('projects');
  return CombineLatestStream.list<QuerySnapshot>([
   for(int i = 0; i < shares.length; i += 10)  //steps of 10, the maximum an array in WhereIn can handle
      ref.where(FieldPath.documentId,
         whereIn: shares.getRange(i,
           i+10 > shares.length ? i + (shares.length % 10) : i+10).toList() // at the end if the array is not a multiple of 10 return the remainder
         ).snapshots()
  ]).map((event) => 
    event.expand((qDocument) => qDocument.docs.map((DocumentSnapshot doc) => doc.data())).toList()
  );
}

这篇关于检索文档列表,仅限于数组中的ID-进行后续操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆