There are couple ways to limit the size of a collection, and in turn the database it’s part of.
First. Capped collection. If the upper-bound limit of the data to be stored is known ahead of it’s creation time then a fixed-size capped collection type would ensure the size will always be less than the max size. Capped collection achieves this by replacing the oldest documents when the collection gets full.
Second. TTL indexes. It’s a type of index that uses date field, or an array of date values to expire/delete documents. A background thread in
mongod process will make sure the documents are deleted when they are past their expiration date. This is helpful if the number of writes are predictable, or else, the storage could fill up based on the frequency of the writes and bursting.
There are some nuances and trade-offs with the above two methods. For more details, please take a look at the documentation. Hopefully, they fit your use case.