Limit number of documents In Firestone for a user

Hello,

I would like to implement the “Recently viewed” for each user in my project. I would just duplicate the viewed object in the users collection with a date when they click the button to see it, and then list that generated collection. But I want to limit the number of documents in that collection to, let’s say, 6 documents. How could I do it to achieve this limit so that when the user saves the seventh doc it will delete the first one and save the new one?

Thank you!

I think the proper way is to create a cloud function which triggers every time a new document is added to “recently viewed” collection and deletes the oldest one if collection document count is greater than 5

Is there a workaround if I don’t have Cloud Functions?

Then you’d need to do the same logic in client side by using some script. I would still suggest to use Cloud function for this: cloud function mechanism isdesigned to solve just this kind of backend logic.

This template looks a similar kind of use case as yours: Limit the number of child nodes in a Firebase database.