Database Reference
In-Depth Information
D ATA FILES AND ALLOCATION
When you create a database, MongoDB allocates a set of data files on disk. All collec-
tions, indexes, and other metadata for the database are stored in these files. The data
files reside in whichever directory you've designated as the dbpath when starting
n mongod . When left unspecified, mongod stores all its files in /data/db. 3 Let's see how
this directory looks after creating the garden database:
$ cd /data/db
$ ls -al
drwxr-xr-x 6 kyle admin 204 Jul 31 15:48 .
drwxrwxrwx 7 root admin 238 Jul 31 15:46 ..
-rwxr-xr-x 1 kyle admin 67108864 Jul 31 15:47 garden.0
-rwxr-xr-x 1 kyle admin 134217728 Jul 31 15:46 garden.1
-rwxr-xr-x 1 kyle admin 16777216 Jul 31 15:47 garden.ns
-rwxr-xr-x 1 kyle admin 6 Jul 31 15:48 mongod.lock
First note the mongod.lock file, which stores the server's process ID . 4 The database
files themselves are all named after the database they belong to. garden.ns is the first
file to be generated. The file's extension, ns , stands for namespaces . Every collection
and index in a database gets its own namespace, and the metadata for each
namespace is stored in this file. By default, the .ns file is fixed to 16 MB, which lets it
store approximately 24,000 namespaces. This means that the sum of the number of
indexes and collections in your database can't exceed 24,000. You're not likely to
need anywhere close to this number of collections or indexes. But on the off chance
that you need even more, you can makes the file larger by using the --nssize server
option.
In addition to creating the namespace file, MongoDB allocates space for the col-
lections and indexes in files ending with incrementing integers starting with 0. Study
the directory listing and you'll see two core data files, the 64 MB garden.0 and the
128 MB garden.1. The initial size of these files often comes as a shock to new users.
But MongoDB favors this preallocation to ensure that as much data as possible will be
stored contiguously. This way, when you query and update the data, those operations
are more likely to occur in proximity, rather than being spread across the disk.
As you add data to your database, MongoDB continues to allocate more data files.
Each new data file gets twice the space of the previously allocated file until the largest
preallocated size of 2 GB is reached. Thus, garden.2 will be 256 MB, garden.3 will use
512 MB, and so forth. The assumption here is that, if the total data size is growing at a
constant rate, the data files should be allocated increasingly, which is a pretty standard
allocation strategy. Certainly one consequence is that the difference between allo-
cated space and actual space used can be high. 5
3
On Windows, it's c:\data\db.
4
Never delete or alter the lock file unless you're recovering from an unclean shutdown. If you start mongod
and get an error message about the lock file, there's a good chance that you've shut down uncleanly, and you
may have to initiate a recovery process. We discuss this further in chapter 10.
5
This may present a problem in deployments where space is at a premium. For those situations, you may use
some combination of the --noprealloc and --smallfiles server options.
Search WWH ::




Custom Search