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

查看:102
本文介绍了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:

现在,我想查询项目"集合,并仅返回给定用户共享"列表中的项目.首先,如何获取共享列表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)

如果列表中有10个或更少的共享ID,则可以使用输入"查询以从项目中找到匹配项,它将不再起作用.

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天全站免登陆