Flutter Firebase:检索文档列表,仅限于数组中的 ID? [英] Flutter Firebase: Retrieve a list of documents, limited to IDs in an array?

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

问题描述

我正在开发一个 Flutter 应用程序,每个用户都可以在其中创建项目,并与其他用户共享项目.我创建了一个共享"集合,其中每个用户的 ID 都是一个文档,在该文档中,与该用户共享的所有项目 ID 都像这样收集,并带有一个布尔值,表示共享是否已尚未接受:

I'm working on a Flutter app where each user can create projects, and share projects with other users. I've created a 'shares' collection, where each user's ID is a document, and within that document, all project IDs that have been shared with that user are collected like so, with a boolean that represents whether or not the share has been accepted yet:

接下来,我创建了项目本身的集合,如下所示:

Next, I created a collection of the projects themselves, like so:

现在,我想查询projects"集合并仅返回给定用户shares"列表中的项目.首先,如何获取共享列表 ID 中的每个文档?其次,是否可以使用 .where() 子句将该 ID 与 List 的内容进行比较?

Now, I'd like to query the 'projects' collection and return only the projects that are in a given user's 'shares' list. First off, how can I get each document in the share list's ID? And secondly, is it possible to compare that ID to the contents of a List using a .where() clause?

我一直在尝试这样的事情,但无济于事:

I've been trying something like this, but to no avail:

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

我也试过这个:

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

我正在尝试做的事情有可能吗?我已经被这两天搞砸了,我的头要爆炸了.任何帮助将不胜感激.提前致谢.

Is what I'm trying to do even possible? I've been messing with this for two days and my head's exploding. Any help would be greatly appreciated. Thanks in advance.

推荐答案

首先,如何获取共享列表 ID 中的每个文档?

First off, how can I get each document in the share list's ID?

为此,您需要实际查询整个集合.您可以迭代结果以收集每个文档的 ID.没有简单的方法可以直接从 Web 和移动客户端代码获取 ID 列表.请参阅:如何获得Cloud Firestore 集合中的文档 ID 列表?

For this, you're required to actually query the entire collection. You can iterate the results to collect the IDs of each document. There is no easy way to just get a list of IDs directly from web and mobile client code. See: How to get a list of document IDs in a collection Cloud Firestore?

其次,是否可以使用 .where() 子句将该 ID 与 List 的内容进行比较?

And secondly, is it possible to compare that ID to the contents of a List using a .where() clause?

如果您在内存中有一个可以是任意长度的文档 ID 字符串列表,您将需要为projOwner"执行查询过滤项目.对于每个单独的 ID.Firestore 中没有类似 SQL 的联接,因此您不能通过单个查询将两个集合简单地联接在一起.

If you have a list of document ID strings in memory that could be any length, you will need to perform a query filtering projects for "projOwner" for each individual ID. There are no SQL-like joins in Firestore, so you can't simply join the two collections together with a single query.

这里是你如何做一个 - 你必须调出字段的名称进行过滤:

Here's how you do a single one - you have to call out the name of the field to filter on:

firestore
    .collection("projects")
    .where("projOwner", isEqualTo: id)

如果列表中的共享 ID 不超过 10 个,则可以使用in"查询以从项目中查找匹配项,它将不再适用.

If you have 10 or less share IDs in the list, you can use an "in" query to find matches from projects, and it will not work with any more.

firestore
    .collection("projects")
    .where("projOwner", whereIn: listOfIds)

因此,如果您认为该列表可能超过 10 个,您应该首先对每个共享 ID 执行单独的查询.

So, if you think the list could ever be larger than 10, you should just start by performing individual queries for each share ID.

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

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