Can I limit size each database on my server?


I have downloaded mongodb community server version 4.4. I have run according to the instructions, created several databases with collections. I wanted to ask if it is possible to limit each database to a size like 2GB? So that the sum of size all collections in the database < my limit.

I looked through the forums but only found a solution involving “storage.mmapv1.smallFiles” which was removed from version 4.4.

Hi @Mateusz_Gajewski, welcome to the community. Glad to have you here.

Why do you want to limit db size? I’m curious to know more about your use case.


I share the databases with other users, I dont want anyone to use all the memory allocated for the mongodb. I want to protect the server from filling up all the memory
For example, I made a mistake in my script and put n (while true) documents in a collection - each weighing 15Mb.

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.