Databases Reference
In-Depth Information
Collections
A collection is a group of documents. If a document is the MongoDB analog of a row
in a relational database, then a collection can be thought of as the analog to a table.
Schema-Free
Collections are schema-free . This means that the documents within a single collection
can have any number of different “shapes.” For example, both of the following docu-
ments could be stored in a single collection:
{"greeting" : "Hello, world!"}
{"foo" : 5}
Note that the previous documents not only have different types for their values (string
versus integer) but also have entirely different keys. Because any document can be put
into any collection, the question often arises: “Why do we need separate collections at
all?” It's a good question—with no need for separate schemas for different kinds of
documents, why should we use more than one collection? There are several good
reasons:
• Keeping different kinds of documents in the same collection can be a nightmare
for developers and admins. Developers need to make sure that each query is only
returning documents of a certain kind or that the application code performing a
query can handle documents of different shapes. If we're querying for blog posts,
it's a hassle to weed out documents containing author data.
• It is much faster to get a list of collections than to extract a list of the types in a
collection. For example, if we had a type key in the collection that said whether
each document was a “skim,” “whole,” or “chunky monkey” document, it would
be much slower to find those three values in a single collection than to have three
separate collections and query for their names (see “Subcollections”
on page 8 ).
• Grouping documents of the same kind together in the same collection allows for
data locality. Getting several blog posts from a collection containing only posts will
likely require fewer disk seeks than getting the same posts from a collection con-
taining posts and author data.
• We begin to impose some structure on our documents when we create indexes.
(This is especially true in the case of unique indexes.) These indexes are defined
per collection. By putting only documents of a single type into the same collection,
we can index our collections more efficiently.
As you can see, there are sound reasons for creating a schema and for grouping related
types of documents together. MongoDB just relaxes this requirement and allows de-
velopers more flexibility.
 
Search WWH ::




Custom Search