如何基于数组中的多个值过滤快照流? [英] How to filter a stream of snapshots based on multiple values in an array?

查看:67
本文介绍了如何基于数组中的多个值过滤快照流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这场风暴和数据库管理方面,我是一个新手.

I am very new in this firestorm and database management.

在我的代码中,网上论坛集合中有很多网上论坛文件现在我要做的是,向用户集合中的某个人展示他所连接的特定组.

In my code, there are plenty of group documents in a groups collection Now what I want to do is, show a person from users collection the specific groups he is connected to.

这是组uid的外观:

here is how the groups uids look:

这是用户文档的外观:

here is how the document of user looks:

我用来创建组文档的代码是创建文档后,我将新文档的uid添加到用户数组中,称为groups.

The code I use to create the group document is this after creating the document I add the uid of the new document to the user's array called groups.

Future createNewGroupData(String groupName) async {
    String _userID = await getUID();
    return await groupCollection.add({
      'creatorUID': _userID,
      'groupName': groupName,
      'teachers': [_userID],
    }).then((ref) => {
      userCollection.document(_userID).updateData({'groups': FieldValue.arrayUnion([ref.documentID])})
    });

这是我为每个用户创建文档的结构,

this is the structure of how I create the document for each user,

Future updateUserData(int avatarID, String firstName, String lastName, String status, List groups, String school) async {
    return await userCollection.document(uid).setData({
      'avatarID': avatarID.round(),
      'firstName': firstName,
      'lastName': lastName,
      'status': status,
      'groups': groups,
      'school': school,
    });
  }

我是如何实现将用户添加到组中的.该组的uid附加在该用户文档下的数组中.

How I implemented that was when the user was added to a group. The uid of that group was appended in an array under that user's documents.

现在,我要执行的操作是获取该组快照的流,仅该用户数组中存在uid.我真的找不到任何实现方法.

Now what I want to do is get a stream of snapshots of the groups only uids are present in that user's array. I can not really find any way to implement that.

推荐答案

如果我正确理解,您希望加载其文档ID与您在用户的 groups 字段中找到的文档ID相匹配的分组文档.在这种情况下,您正在寻找 的组合FieldPath.documentId in 查询.

If I understand correctly you want to load the group documents whose document ID matches what you find in the groups field of a user. If that is the case, you are looking for a combination of FieldPath.documentId and an in query.

类似的东西:

Firestore.instance.collection('groups').where(FieldPath.documentId, whereIn: listOfUids)

如Firestore文档中的查询限制所示最多只能使用10个文档ID:

As shown in the Firestore documentation on query limitations this will only work for up to 10 document IDs:

Cloud Firestore对逻辑 OR 查询提供了有限的支持. in array-contains-any 运算符支持最多10个相等的逻辑 OR ( == )或 array-contains 条件.对于其他情况,请为每个 OR 条件创建一个单独的查询,然后将查询结果合并到您的应用中.

Cloud Firestore provides limited support for logical OR queries. The in and array-contains-any operators support a logical OR of up to 10 equality (==) or array-contains conditions on a single field. For other cases, create a separate query for each OR condition and merge the query results in your app.

如果您有更多的文档ID,则需要针对每个(最多10个)激发一个单独的查询.

If you have more document IDs, you will need to fire a separate query for each (up to) 10.

另请参阅:

这篇关于如何基于数组中的多个值过滤快照流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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