I haven’t seen anything in the docs suggesting it, but does anyone know if there is an absolute limit to the total number of collections that a database can have?
I don’t know if there is any info about it in the official documentation. But, there are quite a few posts on the net with some information regarding. Here is one at StackOverflow: MongoDB - Max number of collections.
Welcome to the MongoDB Community forums!
I’m not aware of any coded limit to the number of collections. There is a practical limit based on the number of characters allowed in a namespace (details available here) but you should never reach anywhere near that many collections.
Though there may not be a hard limit, I’d advise against having more than about 150k active collections. It’s a general rule of thumb and not a hard rule. I also want to re-iterate that I’m talking about active collections (i.e. ones that receive read or write operations, statistic collection and monitoring commands, administrative commands, etc).
WiredTiger requires a file per collection and an additional file for every index. That means, at a minimum, each collection requires two files. Working with a collection in some way forces WiredTiger to open those files, which generates a file handle. I won’t get into the technical details (about dhandles, ulimits, etc) but it’s typical to start seeing operating system enforced filehandle limits around 64k collections. Overcoming that, more active collections means more active file handles. WiredTiger has to maintain those file handles, which becomes burdensome during checkpoints (which occurs every sixty seconds) and result in performance degradations.
I hope this helps clarify your question!
@Justin regarding the answer you gave about the performance impact due to the number of file handles open. Does this apply if there is a large number of databases as well? for example if we were to have 64k collections vs 64k database would the performance impact be the same?
I cannot point you to official documentation but I am pretty sure that it would be the same.
1 collection x 65k databases = 65k collections
65k collections x 1 database = 65k collections
Since one collection is at lease 1 file you should end up with the same number of files.
There was an interesting presentation about supporting 1m collections in 2017.
Unfortunately, development has stalled since then…