About this Book – MongoDB in Action

About this Book

This book is for application developers and DBAs wanting to learn MongoDB from the ground up. If you’re new to MongoDB, you’ll find in this book a tutorial that moves at a comfortable pace. If you’re already a user, the more detailed reference sections in the book will come in handy and should fill any gaps in your knowledge. In terms of depth, the material should be suitable for all but the most advanced users.

The code examples are written in JavaScript, the language of the MongoDB shell, and Ruby, a popular scripting language. Every effort has been made to provide simple but useful examples, and only the plainest features of the JavaScript and Ruby languages are used. The main goal is to present the MongoDB API in the most accessible way possible. If you have experience with other programming languages, you should find the examples easy to follow.

One more note about languages. If you’re wondering, “Why couldn’t this book use language X?” you can take heart. The officially supported MongoDB drivers feature consistent and analogous APIs. This means that once you learn the basic API for one driver, you can pick up the others fairly easily. To assist you, this book provides an overview of the PHP, Java, and C++ drivers in appendix D.

How to use this book

This book is part tutorial, part reference. If you’re brand new to MongoDB, then reading through the book in order makes a lot of sense. There are numerous code examples that you can run on your own to help solidify the concepts. At minimum, you’ll need to install MongoDB and optionally the Ruby driver. Instructions for these installations can be found in appendix A.

If you’ve already used MongoDB, then you may be more interested in particular topics. Chapters 710 and all of the appendixes stand on their own and can safely be read in any order. Additionally, chapters 46 contain the so-called “nuts and bolts” sections, which focus on fundamentals. These also can be read outside the flow of the surrounding text.


This book is divided into three parts.

Part 1 is an end-to-end introduction to MongoDB. Chapter 1 gives an overview of MongoDB’s history, features, and use cases. Chapter 2 teaches the database’s core concepts through a tutorial on the MongoDB command shell. Chapter 3 walks through the design of a simple application that uses MongoDB on the back end.

Part 2 is an elaboration of the MongoDB API presented in part 1. With a specific focus on application development, the three chapters in part 2 progressively describe a schema and its operations for an e-commerce app. Chapter 4 delves into documents, the smallest unit of data in MongoDB, and puts forth a basic e-commerce schema design. Chapters 5 and 6 then teach you how to work with this schema by covering queries and updates. To augment the presentation, each of the chapters in part 2 contains a detailed breakdown of its subject matter.

Part 3 focuses on performance and operations. Chapter 7 is a thorough study of indexing and query optimization. Chapter 8 concentrates on replication, with strategies for deploying MongoDB for high availability and read scaling. Chapter 9 describes sharding, MongoDB’s path to horizontal scalability. And chapter 10 provides a series of best practices for deploying, administering, and troubleshooting MongoDB installations.

The book ends with five appendixes. Appendix A covers installation of MongoDB and Ruby (for the driver examples) on Linux, Mac OS X, and Windows. Appendix B presents a series of schema and application design patterns, and it also includes a list of anti-patterns. Appendix C shows how to work with binary data in MongoDB and how to use GridFS, a spec implemented by all the drivers, to store especially large files in the database. Appendix D is a comparative study of the PHP, Java, and C++ drivers. Appendix E shows you how to use spatial indexing to query on geo-coordinates.

Code conventions and downloads

All source code in the listings and in the text is presented in a fixed-width font, which separates it from ordinary text.

Code annotations accompany some of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow in the text.

As an open source project, 10gen keeps MongoDB’s bug tracker open to the community at large. At several points in the book, particularly in the footnotes, you’ll see references to bug reports and planned improvements. For example, the ticket for adding full-text search to the database is SERVER-380. To view the status of any such ticket, point your browser to http://jira.mongodb.org, and enter the ticket ID in the search box.

You can download the book’s source code, with some sample data, from the book’s site at http://mongodb-book.com as well as from the publisher’s website at http://manning.com/MongoDBinAction.

Software requirements

To get the most out of this book, you’ll need to have MongoDB installed on your system. Instructions for installing MongoDB can be found in appendix A and also on the official MongoDB website (http://mongodb.org).

If you want to run the Ruby driver examples, you’ll also need to install Ruby. Again, consult appendix A for instructions on this.

Author Online

The purchase of MongoDB in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and other users. To access and subscribe to the forum, point your browser to www.manning.com/MongoDBinAction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue between individual readers and between readers and the author can take place. It’s not a commitment to any specific amount of participation on the part of the author, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking him some challenging questions, lest his interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.