2
votes

Testing Collection Group following the documentation (https://firebase.google.com/docs/firestore/security/rules-query#collection_group_queries_and_security_rules)

Firestore Security Rule

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   match /forums/{forumid}/posts/{post} {
      allow read: if true;
    }
  }
}

Query On Android App (Using Firestore 19.0.1)

FirebaseFirestore.getInstance().collectionGroup("posts").get().addOnSuccessListener { queryDocumentSnapshots ->
    Log.d(TAG, "queryDocumentSnapshots " + queryDocumentSnapshots.size())

}.addOnFailureListener {
    Log.d(TAG, "exception" + it)
}

getting exception PERMISSION_DENIED: Missing or insufficient permissions.

1
Please add your database structure.Alex Mamo

1 Answers

10
votes

you need to add a wild card variable in the path to make it work for collection group queries. That is in the documentation if you read more carefully :)

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   match /{path=**}/posts/{post} {
      allow read: if true;
    }
  }
}