1

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.

here is how the groups uids look: here is how the groups uids look

here is how the document of user looks: here is how the document of user looks

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,
    });
  }

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.

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.

1

1 Answer 1

1

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.

Something like:

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

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

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.

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

Also see:

Sign up to request clarification or add additional context in comments.

1 Comment

I am right now implementing to see if it works for me or not. I am actually very beginner soi used this approach to reference the groups by the user. Do you know any other better way to approach this problem? it will be very helpful then!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.