How to Watch an Entire Firestore Collection

Watch Entire Firestore Collection

For the last few years, Google’s Firebase offering has been gaining steady popularity. Firebase allows developers to build highly scalable applications with the ability to have realtime databases. Within the package, Google offers Cloud Firestore as its flagship NoSQL cloud database solution. One of the database’s notable features is that your frontends can get realtime updates from the database without making a direct query. There are multiple ways of setting this up, but some developers have wondered if it was possible to watch the entire Firestore collection at once. Here is how to watch an entire Firestore collection. 

How to Watch an Entire Firestore Collection

Typically in Firestore, you will be watching updates on a specific document using the .onSnapshot() method. Other times you can watch multiple documents by using the .where() and .onSnapshot() methods inline.

However, for some projects, it makes sense for us to watch for changes on an entire collection. Meaning that anytime any documents get created, deleted, or update, our realtime updates will trigger.

This type of setup is straightforward to do. We will simply put a snapshot trigger on the collection itself with the .doc() or .where() method. In this example, our collection is called “triggers”.

db.collection("triggers").onSnapshot(() => { //DO SOMETHING HERE })

That’s all! With that simple code snippet, Firebase will monitor the entire Firestore collection in realtime and execute your code whenever a change occurs.

Word of Caution

Know that you know how to watch an entire Firestore collection we should give you a word of caution. Keep a close eye on your read quotas on Firebase. If your collection starts to rise in transactions, any apps that are watching your entire collection will begin to consume tons of reads. Make sure you understand the potential consumption issues, especially if you are running the Firebase pay-as-you-go Blaze plan. Depending on your project, it might be smart to separate out your high throughput collections so the real-time monitoring costs won’t be exaggerated. Happy Coding!