Databases Reference
In-Depth Information
Naming
A collection is identified by its name. Collection names can be any UTF-8 string, with
a few restrictions:
• The empty string ("") is not a valid collection name.
• Collection names may not contain the character \0 (the null character) because
this delineates the end of a collection name.
• You should not create any collections that start with system. , a prefix reserved for
system collections. For example, the system.users collection contains the database's
users, and the system.namespaces collection contains information about all of the
database's collections.
• User-created collections should not contain the reserved character $ in the name.
The various drivers available for the database do support using $ in collection
names because some system-generated collections contain it. You should not use
$ in a name unless you are accessing one of these collections.
Subcollections
One convention for organizing collections is to use namespaced subcollections sepa-
rated by the . character. For example, an application containing a blog might have a
collection named blog.posts and a separate collection named blog.authors . This is for
organizational purposes only—there is no relationship between the blog collection (it
doesn't even have to exist) and its “children.”
Although subcollections do not have any special properties, they are useful and incor-
porated into many MongoDB tools:
• GridFS, a protocol for storing large files, uses subcollections to store file metadata
separately from content chunks (see Chapter 7 for more information about
GridFS).
• The MongoDB web console organizes the data in its DBTOP section by
subcollection (see Chapter 8 for more information on administration).
• Most drivers provide some syntactic sugar for accessing a subcollection of a given
collection. For example, in the database shell, db.blog will give you the blog col-
lection, and db.blog.posts will give you the blog.posts collection.
Subcollections are a great way to organize data in MongoDB, and their use is highly
recommended.
Databases
In addition to grouping documents by collection, MongoDB groups collections into
databases . A single instance of MongoDB can host several databases, each of which can
be thought of as completely independent. A database has its own permissions, and each
 
Search WWH ::




Custom Search