Mongo space preallocation


When creating a database, MongoDB allocates a set of data files on disk, and all collections, indexes, and other metadata of the database are stored in these files. The data files are all placed in the dbpath specified when mongod is started. When dbpath is not specified, mongod saves all files in /data/db.
Garden.ns is the first generated file. The file extension ns means namespaces, which means namespaces. Each collection and index in the database has its own namespace, and the metadata of each namespace is stored in this file. By default, the. ns file size is fixed at 16 MB, which can store approximately 24,000 namespaces. In other words, the total number of indexes and collections in the database cannot exceed 24,000. It is almost impossible for us to use so many sets and indexes, but if necessary, we can use the –nssize server option to make the file larger.
MongoDB favors this preallocation approach, which allows data to be stored as continuously as possible. As a result, when querying and updating data, these operations can be closer together rather than scattered around the disk.
MongoDB will continue to allocate more data files when adding data to the database. Each new data file is twice the size of the previous allocated file until the upper limit of the preallocated file size is reached-2 GB, i.e. garden.2 will be 256 MB, garden.3 512 MB, and so on. This is based on the assumption that if the total data size increases at a constant rate, the space allocated for data files should be gradually increased, which is a fairly standard allocation strategy. Of course, one of the consequences of this is that there will be a big gap between the space allocated and the space actually used.