Database Reference
In-Depth Information
You can always check the amount of space used versus allocated using the
stats
command:
> db.stats()
{
"collections" : 3,
"objects" : 10004,
"avgObjSize" : 36.005,
"dataSize" : 360192,
"storageSize" : 791296,
"numExtents" : 7,
"indexes" : 1,
"indexSize" : 425984,
"fileSize" : 201326592,
"ok" : 1
}
In this example, the
fileSize
field indicates the total size of files allocated for this
database. This is simply the sum of the sizes of the garden database's two data files,
garden.0 and garden.1. Trickier is the difference between
dataSize
and
storageSize
.
The former is the actual size of the
BSON
objects in the database; the latter includes
extra space reserved for collection growth and also unallocated deleted space.
6
Finally, the
indexSize
value shows the total size of indexes for this database. It's
important to keep an eye on total index size, as database performance will be best
when all utilized indexes can fit in
RAM
. I'll elaborate on this in chapters 7 and 10
when presenting techniques for troubleshooting performance issues.
4.3.2
Collections
Collections are containers for structurally or conceptually similar documents. Here,
I'll describe creating and deleting collections in more detail. Then I'll present
MongoDB's special capped collections, and we'll look at some examples of how the
core server uses collections internally.
M
ANAGING
COLLECTIONS
As you saw in the previous section, you create collections implicitly by inserting docu-
ments into a particular namespace. But because more than one collection type exists,
MongoDB also provides a command for creating collections. From the shell:
db.createCollection("users")
When creating a standard collection, you have the option of preallocating a specific
number of bytes. This usually isn't necessary but can be done like so:
db.createCollection("users", {size: 20000})
Collection names may contain numbers, letters, or
.
characters, but must begin with
a letter or number. Internally, a collection name is identified by its namespace name,
6
Technically, collections are allocated space inside each data file in chunks called
extents
. The
storageSize
is the total space allocated for collection extents.