Book Image

MongoDB Data Modeling

By : Wilson da Rocha França
Book Image

MongoDB Data Modeling

By: Wilson da Rocha França

Overview of this book

<p>This book covers the basic concepts in data modeling and also provides you with the tools to design better schemas. With a focus on data usage, this book will cover how queries and indexes can influence the way we design schemas, with thorough examples and detailed code.</p> <p>The book begins with a brief discussion of data models, drawing a parallel between relational databases, NoSQL, and consequently MongoDB. Next, the book explains the most basic MongoDB concepts, such as read and write operations, indexing, and how to design schemas by knowing how applications will use the data. Finally, we will talk about best practices that will help you optimize and manage your database, presenting you with a real-life example of data modeling on a real-time logging analytics application.</p>
Table of Contents (16 chapters)

Preface

Even today, it is still quite common to say that computer science is a young and new field. However, this statement becomes somewhat contradictory when we observe other fields. Unlike other fields, computer science is a discipline that is continually evolving above the normal speed. I dare say that computer science has now set the path of evolution for other fields such as medicine and engineering. In this context, database systems as an area of the computer science discipline has not only contributed to the growth of other fields, but has also taken advantage itself of the evolution and progress of many areas of technology such as computer networks and computer storage.

Formally, database systems have been an active research topic since the 1960s. Since then, we have gone through a few generations, and big names in the IT industry have emerged and started to dictate the market's tendencies.

In the 2000s, driven by the world's Internet access growth, which created a new network traffic profile with the social web boom, the term NoSQL became common. Considered by many to be a paradoxical and polemic subject, it is seen by some as a new technology generation that has been developed in response to all changes we have experienced in the last decade.

MongoDB is one of these technologies. Born in the early 2000s, it became the most popular NoSQL database in the world. Not only the most popular database in the world, since February 2015, MongoDB became the fourth most popular database system according to the DB-Engines ranking (http://db-engines.com/en/), surpassing the well-known PostgreSQL database.

Nevertheless, popularity should not be confused with adoption. Although the DB-Engines ranking shows us that MongoDB is responsible for some traffic on search engines such as Google, has job search activity, and has substantial social media activity, we can not state how many applications are using MongoDB as a data source. Indeed, this is not exclusive to MongoDB, but is true of every NoSQL technology.

The good news is that adopting MongoDB has not been a very tough decision to make. It's open source, so you can download it free of charge from MongoDB Inc. (https://www.mongodb.com), where you can find extensive documentation. You also can count on a big and growing community, who, like you, are always looking for new stuff on books, blogs, and forums; sharing knowledge and discoveries; and collaborating to add to the MongoDB evolution.

MongoDB Data Modeling was written with the aim of being another research and reference source for you. In it, we will cover the techniques and patterns used to create scalable data models with MongoDB. We will go through basic database modeling concepts, and provide a general overview focused on modeling in MongoDB. Lastly, you will see a practical step-by-step example of modeling a real-life problem.

Primarily, database administrators with some MongoDB background will take advantage of MongoDB Data Modeling. However, everyone from developers to all the curious people that downloaded MongoDB will make good use of it.

This book focuses on the 3.0 version of MongoDB. MongoDB 3.0, which was long awaited by the community, is considered by MongoDB Inc. as its most significant release to date. This is because, in this release, we were introduced to the new and highly flexible storage architecture, WiredTiger. Performance and scalability enhancements intend to strengthen MongoDB's emphasis among database systems technologies, and position it as the standard database for modern applications.

What this book covers

Chapter 1, Introducing Data Modeling, introduces you to basic data modeling concepts and the NoSQL universe.

Chapter 2, Data Modeling with MongoDB, gives you an overview of MongoDB's document-oriented architecture and presents you with the document, its characteristics, and how to build it.

Chapter 3, Querying Documents, guides you through MongoDB APIs to query documents and shows you how the query affects our data modeling process.

Chapter 4, Indexing, explains how you can improve the execution of your queries and consequently change the way we model our data by making use of indexes.

Chapter 5, Optimizing Queries, helps you to use MongoDB's native tools to optimize your queries.

Chapter 6, Managing the Data, focuses on the maintenance of data. This will teach you how important it is to look at your data operations and administration before beginning the modeling of data.

Chapter 7, Scaling, shows you how powerful the autosharing characteristic of MongoDB can be, and how we think our data model is distributed.

Chapter 8, Logging and Real-time Analytics with MongoDB, takes you through an schema design of a real-life problem example.

What you need for this book

To successfully understand every chapter on this book, you need access to a MongoDB 3.0 instance.

You can choose where and how you will run it. We know that there are many ways you can do it. So, pick one.

To execute the queries and commands, I recommend you do this on a mongo shell. Every time I do this outside the mongo shell, I will warn you.

In Chapter 8, Logging and Real-time Analytics with MongoDB, you will need to have Node.js installed on your machine and it should have access to your MongoDB instance.

Who this book is for

This book assumes that you have already had first contact with MongoDB and have some experience with JavaScript. The book is for database administrators, developers, or anyone that is looking for some data modeling concepts and how they fit into the MongoDB world. It does not teach you JavaScript or how to install MongoDB on your machine. If you are a MongoDB beginner, you can find good Packt Publishing books that will help you to get enough experience to better understand this book.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can store the relationship in the Group document."

A block of code is set as follows:

  collection.update({resource: resource, date: today},
    {$inc : {daily: 1}}, {upsert: true},
    function(error, result){
      assert.equal(error, null);
      assert.equal(1, result.result.n);
      console.log("Daily Hit logged");
      callback(result);
  });

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var logMinuteHit = function(db, resource, callback) {
  // Get the events collection
  var collection = db.collection('events');
  // Get current minute to update
  var currentDate = new Date();
  var minute = currentDate.getMinutes();
  var hour = currentDate.getHours();
  // We calculate the minute of the day
  var minuteOfDay = minute + (hour * 60);
  var minuteField = util.format('minute.%s', minuteOfDay);

Any command-line input or output is written as follows:

db.customers.find(
{"username": "johnclay"},
{_id: 1, username: 1, details: 1}
)

New terms and important words are shown in bold.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.