Database Reference
In-Depth Information
Chapter 3
The Data Model
In the previous chapter, you learned how to install MongoDB on two commonly used platforms (Windows and Linux),
as well as how to extend the database with some additional drivers. In this chapter, you will shift your attention from
the operating system and instead examine the general design of a MongoDB database. Specifically, you'll learn
what collections are, what documents look like, how indexes work and what they do, and finally, when and where to
reference data instead of embedding it. We touched on some of these concepts briefly in Chapter 1, but in this chapter,
we'll explore them in more detail. Throughout this chapter, you will see code examples designed to give you a good
feeling for the concepts being discussed. Do not worry too much about the commands you'll be looking at, however,
because they will be discussed extensively in Chapter 4.
Designing the Database
As you learned in the first two chapters, a MongoDB database is nonrelational and schemaless. This means that a
MongoDB database isn't bound to any predefined columns or datatypes as relational databases are (such as MySQL).
The biggest benefit of this implementation is that working with data is extremely flexible because there is no
predefined structure required in your documents.
To put it more simply: you are perfectly capable of having one collection that contains hundreds or even
thousands of documents that all carry a different structure—without breaking any of the MongoDB databases rules.
One of the benefits of this flexible schemaless design is that you won't be restricted when programming in
a dynamically typed language such as Python or PHP. Indeed, it would be a severe limitation if your extremely
flexible and dynamically capable programming language couldn't be used to its full potential because of the innate
limitations of your database.
Let's take another glance at what the data design of a document in MongoDB looks like, paying particular
attention to how flexible data in MongoDB is compared to data in a relational database. In MongoDB, a document
is an item that contains the actual data, comparable to a row in SQL. In the following example, you will see how two
completely different types of documents can coexist in a single collection named Media (note that a collection is
roughly equivalent to a table in the world of SQL):
{
"Type": "CD",
"Artist": "Nirvana",
"Title": "Nevermind",
"Genre": "Grunge",
"Releasedate": "1991.09.24",
 
Search WWH ::




Custom Search