Copyright......Page 6 Table of Contents......Page 7 Foreword......Page 13 Advanced Usage......Page 15 Conventions Used in This Book......Page 16 Safari® Books Online......Page 17 Acknowledgments from Kristina......Page 18 Acknowledgments from Michael......Page 19 A Rich Data Model......Page 21 Tons of Features.........Page 22 Simple Administration......Page 23 But Wait, That’s Not All.........Page 24 Documents......Page 25 Schema-Free......Page 27 Databases......Page 28 Getting and Starting MongoDB......Page 30 Running the Shell......Page 31 Create......Page 32 Update......Page 33 Tips for Using the Shell......Page 34 Data Types......Page 35 Basic Data Types......Page 36 Numbers......Page 38 Arrays......Page 39 _id and ObjectIds......Page 40 ObjectIds......Page 41 Autogeneration of _id......Page 42 Batch Insert......Page 43 Inserts: Internals and Implications......Page 44 Remove Speed......Page 45 Document Replacement......Page 46 Using Modifiers......Page 47 Getting started with the "$set" modifier......Page 49 Incrementing and decrementing......Page 50 Array modifiers......Page 51 Positional array modifications......Page 54 Modifier speed......Page 55 Upserts......Page 56 The save Shell Helper......Page 57 Updating Multiple Documents......Page 58 Returning Updated Documents......Page 59 The Fastest Write This Side of Mississippi......Page 61 Safe Operations......Page 62 Requests and Connections......Page 63 Introduction to find......Page 65 Specifying Which Keys to Return......Page 66 Query Conditionals......Page 67 OR Queries......Page 68 null......Page 69 Regular Expressions......Page 70 $all......Page 71 The $slice operator......Page 72 Querying on Embedded Documents......Page 73 $where Queries......Page 75 Cursors......Page 76 Limits, Skips, and Sorts......Page 77 Avoiding Large Skips......Page 78 Finding a random document......Page 79 Advanced Query Options......Page 80 Getting Consistent Results......Page 81 Cursor Internals......Page 83 Introduction to Indexing......Page 85 Indexing Keys in Embedded Documents......Page 88 Unique Indexes......Page 89 Using explain and hint......Page 90 Index Administration......Page 95 Changing Indexes......Page 96 Geospatial Indexing......Page 97 Compound Geospatial Indexes......Page 98 The Earth Is Not a 2D Plane......Page 99 distinct......Page 101 group......Page 102 Using a Finalizer......Page 104 MapReduce......Page 106 Example 1: Finding All Keys in a Collection......Page 107 Example 2: Categorizing Web Pages......Page 109 Keeping output collections......Page 110 Using a scope......Page 111 Getting more output......Page 112 Database Commands......Page 113 How Commands Work......Page 114 Command Reference......Page 115 Capped Collections......Page 117 Properties and Use Cases......Page 118 Sorting Au Naturel......Page 119 GridFS: Storing Files......Page 121 Working with GridFS from the MongoDB Drivers......Page 122 Under the Hood......Page 123 db.eval......Page 124 Stored JavaScript......Page 125 Security......Page 126 Example Schema......Page 127 When Should DBRefs Be Used?......Page 128 Starting and Stopping MongoDB......Page 131 Starting from the Command Line......Page 132 File-Based Configuration......Page 133 Monitoring......Page 134 Using the Admin Interface......Page 135 serverStatus......Page 136 Authentication Basics......Page 138 How Authentication Works......Page 140 Data File Backup......Page 141 mongodump and mongorestore......Page 142 fsync and Lock......Page 143 Repair......Page 144 Master-Slave Replication......Page 147 Options......Page 148 Adding and Removing Sources......Page 149 Replica Sets......Page 150 Initializing a Set......Page 152 Nodes in a Replica Set......Page 153 Failover and Primary Election......Page 155 Performing Operations on a Slave......Page 156 Using Slaves for Data Processing......Page 157 The Oplog......Page 158 Replication State and the Local Database......Page 159 Blocking for Replication......Page 160 Changing the Oplog Size......Page 161 Replication with Authentication......Page 162 Autosharding in MongoDB......Page 163 Sharding an Existing Collection......Page 165 How Shard Keys Affect Operations......Page 166 Starting the Servers......Page 167 Sharding Data......Page 168 Many mongos......Page 169 Shards......Page 170 Chunks......Page 171 Removing a shard......Page 172 Using the Java Driver......Page 175 Schema Design......Page 176 Writing This in Java......Page 178 News Aggregator: PHP......Page 179 Mac OS X Install......Page 180 Using the PHP Driver......Page 181 Trees of Comments......Page 182 Installing the Ruby Driver......Page 184 Using the Ruby Driver......Page 185 Custom Form Submission......Page 186 Ruby Object Mappers and Using MongoDB with Rails......Page 187 Using PyMongo......Page 188 Schema......Page 189 Using Analytics Data......Page 190 Other Considerations......Page 191 Choosing a Version......Page 193 Windows Install......Page 194 POSIX (Linux, Mac OS X, and Solaris) Install......Page 195 Installing from a Package Manager......Page 196 Appendix B. mongo: The Shell......Page 197 Shell Utilities......Page 198 BSON......Page 199 Namespaces and Extents......Page 200 Memory-Mapped Storage Engine......Page 201 Index......Page 203 How does MongoDB help you manage a huMONGOus amount of data collected through your web application? With this authoritative introduction, you'll learn the many advantages of using document-oriented databases, and discover why MongoDB is a reliable, high-performance system that allows for almost infinite horizontal scalability. Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for system administrators, and an overview of the concepts and use cases for other people on your project. Learn how easy it is to handle data as self-contained JSON-style documents, rather than as records in a relational database. Explore ways that document-oriented storage will work for your project Learn how MongoDB’s schema-free data model handles documents, collections, and multiple databases Execute basic write operations, and create complex queries to find data with any criteria Use indexes, aggregation tools, and other advanced query techniques Learn about monitoring, security and authentication, backup and repair, and more Set up master-slave and automatic failover replication in MongoDB Use sharding to scale MongoDB horizontally, and learn how it impacts applications Get example applications written in Java, PHP, Python, and Ruby Manage the huMONGOus amount of data collected through your web application with MongoDB. This authoritative introduction—written by a core contributor to the project—shows you the many advantages of using document-oriented databases, and demonstrates how this reliable, high-performance system allows for almost infinite horizontal scalability.This updated second edition provides guidance for database developers, advanced configuration for system administrators, and an overview of the concepts and use cases for other people on your project. Ideal for NoSQL newcomers and experienced MongoDB users alike, this guide provides numerous real-world schema design examples.Get started with MongoDB core concepts and vocabularyPerform basic write operations at different levels of safety and speedCreate complex queries, with options for limiting, skipping, and sorting resultsDesign an application that works well with MongoDBAggregate data, including counting, finding distinct values, grouping documents, and using MapReduceGather and interpret statistics about your collections and databasesSet up replica sets and automatic failover in MongoDBUse sharding to scale horizontally, and learn how it impacts applicationsDelve into monitoring, security and authentication, backup/restore, and other administrative tasks Annotation This guide covers the basic principles and advanced uses of this document-oriented database, and demonstrates why MongoDB is one of the fastest-growing databases. Learn how MongoDB handles data and why MongoDB is scalable, high-performance, and reliable