Why I am able to do CRUD operations at subCollections but not at parentCollection?
Idealy if I am able to read and write at nested sub-collection why not the same at parent collection?
Architecture :
Parent Collection
Sub Collection (nested collection)
Current Firebase Rules:
Firebase Rules
Current Firebase Indexes:
Firebase indexes
Code (Which was working earlier) ❌
FirebaseFirestore.getInstance()
.collection("participants")
.document("123456789")
.get()
.addOnSuccessListener { documentSnapShot ->
// Actual code removed for readibility
Log.d("Screen B", "${documentSnapShot.data}")
}.addOnFailureListener { exception ->
exception.printStackTrace()
}
`
Issue I am facing
The above code is throwing exception :
PERMISSION_DENIED: Missing or insufficient permissions.
Whenever I am trying to access the parent Collection documents using the above code,
firebase is throwing this exception.
Earlier the code was working, until I did some changes at the Firebase Rules.
But weirdly, if I try to access the sub-collection like user-data
collection, I am able to fetch data perfectly.
Code for fetching sub-collection ✅
FirebaseFirestore.getInstance()
.collectionGroup("user_data")
.get()
.addOnSuccessListener { documents ->
// removed actual code for readability
}
.addOnFailureListener {
Log.e("AllParticipantScreen", "Exception : Caught ${it.message}")
}
The Changes at the Firebase rules, before things started to breaking :
- Added rules for collection groups using recurssive wildcards -> document=**
- Made some changes in the
Firebase Indexes
page (img added above) [to implement search functionailty]
Things I tried so far :
- Download and replaced old google-services.json with new one
- Tried and made changes in the Firebase Rules
Images for Firebase Rules I tried before which are not working :
2
Answers
This is the firebase rule, I updated it in order to make things work, Please mark/comment, if I am doing anything wrong or can have some issues later.
[Sharing image of my current Firebase rule.]
If you need to set read and write permission to each document that exists under the
participants
collection until a specific date, then you should consider using the following rules:The above rules will allow the read and write operation until 2024-04-12.