Reputation: 2668
I'm building Flutter app with Firebase Firestore. I'm trying to implement likes feature bug faced some platform limitations.
I have a collection users
.
Each user has likedUsers
collection where I store uid
s of liked users. Also each user has 'likedBy' collection where I store uid
s of other users liked current user.
Firestore is not allowing to run collection group query, so I can't get all users that have uid
of current user in their likedUsers
collection.
What I can do is only to get list of user uid
s from 'likedBy' collection of current user and get users one by one like this:
final QuerySnapshot querySnapshot = await _db
.collection(FirebaseContract.USERS_COL)
.document(uid)
.collection(User.LIKED_BY_COL)
.getDocuments();
querySnapshot.documents.forEach(
(DocumentSnapshot snapshot) {
_db.collection(FirebaseContract.USERS_COL).document(snapshot.documentID).get().then(
(DocumentSnapshot document) {
print(document[User.NAME]);
},
);
},
);
Is there any more elegant, time and cost efficient solution for this problem than this?
Upvotes: 0
Views: 713
Reputation: 392
For now, Firestore mobile API doesn't handle queries by arrays of References (whether of collections or documents) :
Retrieving a list of collections is not possible with the mobile/web client libraries. You should only look up collection names as part of administrative tasks in trusted server environments. If you find that you need this capability in the mobile/web client libraries, consider restructuring your data so that subcollection names are predictable.
This means that the only way to get an array of collections from their references is to enumerate the way you do it.
Upvotes: 1