1

I am using a collection in Firebase Firestore to log some activities but I don't want this log collection to grow forever. Is there a way to set a limit to the number of documents in a collection or a size limit for the whole collection or get a notification if it passes a limit? OR is there a way to automatically delete old documents in a collection just by settings and not writing some cron job or scheduled function? Alternatively, what options are there to create a rotational logging system for client activities in Firebase?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
pref
  • 1,651
  • 14
  • 24

1 Answers1

1

I don't want this log collection to grow forever.

Why not? There are no downsides. In Firestore the performance depends on the number of documents you request and not on the number of documents you search. So it doesn't really matter if you search 10 documents in a collection of 100 documents or in a collection of 100 MIL documents, the response time will always be the same. As you can see, the number of documents within a collection is irrelevant.

Is there a way to set a limit to the number of documents in a collection or a size limit for the whole collection or get a notification if it passes a limit?

There is no built-in mechanism for that. However, you can create one mechanism yourself in a very simple way. Meaning, that you can create a document in which you can increment/decrement a numeric value, each time a document is added or deleted from the collection. Once you hit the limit, you can restrict the addition of documents in that particular collection.

OR is there a way to automatically delete old documents in a collection just by settings and not writing some cron job or scheduled function?

There is also no automatic operation that can help you achieve that. You can either use the solution above and once you hit the limit + 1, you can delete the oldest document. Or you can use a Cloud Function for Firebase to achieve the same thing. I cannot see any reason why you should use a cron job. You can use a Cloud Scheduler to perform some operation at a specific time, but as I understand you want it to happen automatically when you hit the limit.

Alternatively, what options are there to create a rotational logging system for client activities in Firebase?

If you still don't want to have larger collections, maybe you can export the data into a file and add that file to Cloud Storage for Firebase.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193