MongoDB: The Definitive Guide
Kristina Chodorow
Michael Dirolf
Published by OReilly Media
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Foreword
Jeremy Zawodny
Craigslist Software Engineer
In the last 10 years, the Internet has challenged relational databases in ways nobody could have foreseen. Having used MySQL at large and growing Internet companies during this time, Ive seen this happen firsthand. First you have a single server with a small data set. Then you find yourself setting up replication so you can scale out reads and deal with potential failures. And, before too long, youve added a caching layer, tuned all the queries, and thrown even more hardware at the problem.
Eventually you arrive at the point when you need to shard the data across multiple clusters and rebuild a ton of application logic to deal with it. And soon after that you realize that youre locked into the schema you modeled so many months before.
Why? Because theres so much data in your clusters now that altering the schema will take a long time and involve a lot of precious DBA time. Its easier just to work around it in code. This can keep a small team of developers busy for many months. In the end, youll always find yourself wondering if theres a better wayor why more of these features are not built into the core database server.
Keeping with tradition, the Open Source community has created a plethora of better ways in response to the ballooning data needs of modern web applications. They span the spectrum from simple in-memory key/value stores to complicated SQL-speaking MySQL/InnoDB derivatives. But the sheer number of choices has made finding the right solution more difficult. Ive looked at many of them.
I was drawn to MongoDB by its pragmatic approach. MongoDB doesnt try to be everything to everyone. Instead it strikes the right balance between features and complexity, with a clear bias toward making previously difficult tasks far easier. In other words, it has the features that really matter to the vast majority of todays web applications: indexes, replication, sharding, a rich query syntax, and a very flexible data model. All of this comes without sacrificing speed.
Like MongoDB itself, this book is very straightforward and approachable. New MongoDB users can start with and be up and running in no time. Experienced users will appreciate this books breadth and authority. Its a solid reference for advanced administrative topics such as replication, backups, and sharding, as well as popular client APIs.
Having recently started to use MongoDB in my day job, I have no doubt that this book will be at my side for the entire journeyfrom the first install to production deployment of a sharded and replicated cluster. Its an essential reference to anyone seriously looking at using MongoDB.
Preface
How This Book Is Organized
Getting Up to Speed with MongoDB
In also provides a first look at working with MongoDB, getting you started with the database and the shell.
Developing with MongoDB
The next two chapters cover the basic material that developers need to know to work with MongoDB. In , explains how to find documents and create complex queries. This chapter also covers how to iterate through results and options for limiting, skipping, and sorting results.
Advanced Usage
The next three chapters go into more complex usage than simply storing and retrieving data. , is a mishmash of important tidbits that didnt fit into any of the previous categories: file storage, server-side JavaScript, database commands, and database references.
Administration
The next three chapters are less about programming and more about the operational aspects of MongoDB. , describes how to scale MongoDB horizontally: it covers what autosharding is, how to set it up, and the ways in which it impacts applications.
Developing Applications with MongoDB
In , we provide example applications using MongoDB, written in Java, PHP, Python, and Ruby. These examples illustrate how to map the concepts described earlier in the book to specific languages and problem domains.
Appendixes
, details a little about how MongoDB works internally: its storage engine, data format, and wire protocol.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, collection names, database names, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, command-line utilities, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This icon signifies a tip, suggestion, or general note.
Caution
This icon indicates a warning or caution.
Using Code Examples
This book can help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf (OReilly). Copyright 2010 Kristina Chodorow and Michael Dirolf, 978-1-449-38156-1.
If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .
Safari Books Online
Note
Safari Books Online is an on-demand digital library that lets you easily search more than 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
OReilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from OReilly and other publishers, sign up for free at http://my.safaribooksonline.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher: