skip to Main Content

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?

[Sharing images of my Firebase Firestore Database]

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 :

Firebase rule set one
Firebase rule set two

2

Answers


  1. Chosen as BEST ANSWER

    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.]

    Firebase rules updated to make thing work


  2. 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:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /participants/{participantId} {
          allow read, write: if request.time < timestamp.date(2024, 4, 12);
        }
        match /participants/{participantId}/user_data/{userId} {
          allow read, write: if request.time < timestamp.date(2024, 4, 12);
        }
      }
    }
    

    The above rules will allow the read and write operation until 2024-04-12.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search