0

I'm creating a web app for admin with firebase integration, this web app is used by admin to monitor posts or comments posted by user from Android or iOS app developed in ionic.

I'm using Firebase firestore, and following is the database design posts/{postsDocument}/comments/{commentsDocument}.

The comments here is sub-collection for postsDocument that holds all the comments for a particular post. Also postDocument and commentsDocument contains Firebase uid of the user.

My problem is, whether it's possible to fetch all the comments commented by a particular user. I can query a single collection, but in this scenario commentsDocument is a subcollection and i want to fetch all comments across all the post by a user for monitoring purpose.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

2

You can check firebase collection group query

var commentsQuery = db.collectionGroup('comments').where('commentOwnerID', '==', userId);//userId - firebase id of the user who commented
commentsQuery.get().then(function (querySnapshot) {
    querySnapshot.forEach(function (doc) {
        console.log(doc.id, ' => ', doc.data());
    });
});

Also you might have to create index that supports your query, check console for the the link for creating index

ked
  • 2,426
  • 21
  • 24