MongoDB: The Definitive Guide
Kristina Chodorow
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
This book is split up into six sections, covering development, administration, and deployment information.
Getting Started with MongoDB
In explains how to find documents and create complex queries. This chapter also covers how to iterate through results and gives options for limiting, skipping, and sorting results.
Developing with MongoDB
goes over tips for writing an application that works well with MongoDB.
Replication
The replication section starts with covers the administrative aspects of running a replica set.
Sharding
The sharding section starts in covers administering a sharded cluster.
Application Administration
The next two chapters cover many aspects of MongoDB administration from the perspective of your application. explains how MongoDB stores data durably.
Server Administration
The final section is focused on server administration. discusses a number of system settings to keep in mind when deploying MongoDB.
Appendixes
details ow 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, Second Edition by Kristina Chodorow (OReilly). Copyright 2013 Kristina Chodorow, 978-1-449-34468-9.
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 (www.safaribooksonline.com) is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business.
Technology professionals, software developers, web designers, and business and creative professionals use Safari Books Online as their primary resource for research, problem solving, learning, and certification training.
Safari Books Online offers a range of product mixes and pricing programs for organizations, government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like OReilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology, and dozens more. For more information about Safari Books Online, please visit us online.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
OReilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707 829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: