Search code examples
google-cloud-firestorefirebase-security

How to select collections that starts with a certain prefix using Firebase security rules wildcard


I have two sets of collections in firebase. One set that starts with a prefix 'user'

For example

  • /user_invoice
  • /user_report

... etc

and the other set that does not start with any particular prefix.something like

  • config
  • customer

..etc

Is there a way that I can apply a set of security rules using wildcard or any other means for all the collections that starts with 'user_' and another set of rules for other set of collections


Solution

  • You can select specific collections that starts with user_ like this:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{collection=user_**}/{doc} {
          // Rules for collections that does start with user_
        }
        match /{collection=!(user_**)/{doc} {
          // Rules for collections that doesn't start with user_
          
        }
      }
    }
    

    You can read more about control access to specific fields here. I hope it helps