Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit a number of documents in a subcollection in firestore rules

My request is pretty simple in appearence : limiting the number of documents in a subcollection using Firestore Rules.

I already know that it is possible to know the size of an array using .size(). So, you may ask why I am not using an array to store my items ? Well, I want a "normal" user to not be able to update my parent document field but to be able to add a document to my subcollection.

I already tried to do this :

match /slots/{slot} {
      allow read: if true;
      allow write: if getRole() == 'ADMIN';

      match /participants/{participant} {
        allow read: if true;
        allow create: if get(/databases/$(database)/documents/slots/$(slot)/participants).size() < 2;
        allow delete: if participant.data.id == request.auth.uid || getRole() == 'ADMIN';
      }
    }

But get(/databases/$(database)/documents/slots/$(slot)/participants) does not work as get() should only be used for fetching a single document.

Maybe I can try something with cloud functions...

I there any solution to my problem ? Thank you for your help !

like image 260
Anthony Granger Avatar asked Oct 29 '18 09:10

Anthony Granger


People also ask

How do I limit firestore?

To cap your Cloud Firestore usage, set a daily spending limit through App Engine. App Engine allows you to set a daily spending limit on App Engine associated resources, including Cloud Firestore. The App Engine limit does not apply to any other Firebase products.

What is the limited depth of Subcollection in firestore?

I saw on the Firebase Firestore documentation that the limits for the "Maximum depth of subcollections" is 100.

How many documents can I store in firestore?

Your documents should have fewer than 100 fields. Keep in mind the standard limits for Firestore. Pay special attention to the 1 write per second limit for documents and the limit of 1,000,000 concurrent connections per database. These are soft limits that Firestore does not stop you from exceeding.

How do I set rules for firestore database?

To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.


1 Answers

In a general sense (both in database rules and client SDKs) there is no way to know the size of a collection. You have the option of recording that yourself in another document that's managed by Cloud Functions. But bear in mind also that a document is limited to 10 writes per second, so busy collections might not be able to keep in sync with the document used to record size.

You could also use Cloud Functions to remove documents from a collection in response to new documents being added, but the limitation of now knowing the size of the collection will be problematic. It might be better to find a way to query to find documents to delete (for example, by timestamp) rather than depending on the size of the collection.

like image 84
Doug Stevenson Avatar answered Oct 14 '22 16:10

Doug Stevenson