Summary MongoDB in Action is a comprehensive guide to MongoDB for application developers. The book begins by explaining what makes MongoDB unique and describing its ideal use cases. A series of tutorials designed for MongoDB mastery then leads into detailed examples for leveraging MongoDB in e-commerce, social networking, analytics, and other common applications. About the Technology Big data can mean big headaches. MongoDB is a document-oriented database designed to be flexible, scalable, and very fast, even with big data loads. It's built for high availability, supports rich, dynamic schemas, and lets you easily distribute data across multiple servers. About this Book MongoDB in Action introduces you to MongoDB and the document-oriented database model. This perfectly paced book provides both the big picture you'll need as a developer and enough low-level detail to satisfy a system engineer. Numerous examples will help you develop confidence in the crucial area of data modeling. You'll also love the deep explanations of each feature, including replication, auto-sharding, and deployment This book is written for developers. No MongoDB or NoSQL experience required. Purchase of the print book comes with an offer of a free PDF, ePub, and Kindle eBook from Manning. Also available is all code from the book. What's Inside Indexes, queries, and standard DB operations Map-reduce for custom aggregations and reporting Schema design patterns Deploying for scale and high availability Table of Contents PART 1 GETTING STARTED A database for the modern web MongoDB through the JavaScript shell Writing programs using MongoDB PART 2 APPLICATION DEVELOPMENT IN MONGODB Document-oriented data Queries and aggregation Updates, atomic operations, and deletes PART 3 MONGODB MASTERY Indexing and query optimization Replication Sharding Deployment and administration MongoDB in Action 1 brief contents 8 contents 10 preface 16 acknowledgments 18 about this book 19 How to use this book 19 Roadmap 20 Code conventions and downloads 20 Software requirements 21 Author Online 21 about the cover illustration 22 Part 1 Getting started 24 Chapter 1 A database for the modern web 26 1.1 Born in the cloud 28 1.2 MongoDB鈥檚 key features 28 1.2.1 The document data model 28 1.2.2 Ad hoc queries 31 1.2.3 Secondary indexes 33 1.2.4 Replication 33 1.2.5 Speed and durability 34 1.2.6 Scaling 35 1.3 MongoDB鈥檚 core server and tools 36 1.3.1 The core server 37 1.3.2 The JavaScript shell 37 1.3.3 Database drivers 38 1.3.4 Command-line tools 39 1.4 Why MongoDB? 39 1.4.1 MongoDB versus other databases 40 1.4.2 Use cases and production deployments 42 1.5 Tips and limitations 44 1.6 Summary 45 Chapter 2 MongoDB through the JavaScript shell 46 2.1 Diving into the MongoDB shell 47 2.1.1 Starting the shell 47 2.1.2 Inserts and queries 48 2.1.3 Updating documents 49 2.1.4 Deleting data 51 2.2 Creating and querying with indexes 52 2.2.1 Creating a large collection 52 2.2.2 Indexing and explain() 54 2.3 Basic administration 56 2.3.1 Getting database information 56 2.3.2 How commands work 57 2.4 Getting help 58 2.5 Summary 59 Chapter 3 Writing programs using MongoDB 60 3.1 MongoDB through the Ruby lens 61 3.1.1 Installing and connecting 61 3.1.2 Inserting documents in Ruby 62 3.1.3 Queries and cursors 63 3.1.4 Updates and deletes 64 3.1.5 Database commands 65 3.2 How the drivers work 66 3.2.1 Object ID generation 66 3.2.2 BSON 67 3.2.3 Over the network 68 3.3 Building a simple application 70 3.3.1 Setting up 70 3.3.2 Gathering data 71 3.3.3 Viewing the archive 73 3.4 Summary 75 Part 2 Application development in MongoDB 76 Chapter 4 Document-oriented data 78 4.1 Principles of schema design 79 4.2 Designing an e-commerce data model 80 4.2.1 Products and categories 81 4.2.2 Users and orders 84 4.2.3 Reviews 87 4.3 Nuts and bolts: on databases, collections, and documents 88 4.3.1 Databases 88 4.3.2 Collections 90 4.3.3 Documents and insertion 93 4.4 Summary 98 Chapter 5 Queries and aggregation 99 5.1 E-commerce queries 100 5.1.1 Products, categories, and reviews 100 5.1.2 Users and orders 102 5.2 MongoDB鈥檚 query language 104 5.2.1 Query selectors 104 5.2.2 Query options 113 5.3 Aggregating orders 115 5.3.1 Grouping reviews by user 115 5.3.2 Map-reduce for orders by region 117 5.4 Aggregation in detail 118 5.4.1 Maxima and minima 118 5.4.2 Distinct 119 5.4.3 Group 119 5.4.4 Map-reduce 121 5.5 Summary 123 Chapter 6 Updates, atomic operations, and deletes 124 6.1 A brief tour of document updates 125 6.2 E-commerce updates 127 6.2.1 Products and categories 127 6.2.2 Reviews 131 6.2.3 Orders 133 6.3 Atomic document processing 135 6.3.1 Order state transitions 135 6.3.2 Inventory management 137 6.4 Nuts and bolts: MongoDB updates and deletes 141 6.4.1 Update types and options 141 6.4.2 Update operators 142 6.4.3 The findAndModify command 146 6.4.4 Deletes 147 6.4.5 Concurrency, atomicity, and isolation 147 6.4.6 Update performance notes 148 6.5 Summary 149 Part 3 MongoDB mastery 150 Chapter 7 Indexing and query optimization 152 7.1 Indexing theory 153 7.1.1 A thought experiment 153 7.1.2 Core indexing concepts 156 7.1.3 B-trees 159 7.2 Indexing in practice 160 7.2.1 Index types 161 7.2.2 Index administration 163 7.3 Query optimization 167 7.3.1 Identifying slow queries 167 7.3.2 Examining slow queries 170 7.3.3 Query patterns 176 7.4 Summary 178 Chapter 8 Replication 179 8.1 Replication overview 180 8.1.1 Why replication matters 180 8.1.2 Replication use cases 181 8.2 Replica sets 182 8.2.1 Setup 182 8.2.2 How replication works 186 8.2.3 Administration 192 8.3 Master-slave replication 200 8.4 Drivers and replication 200 8.4.1 Connections and failover 200 8.4.2 Write concern 202 8.4.3 Read scaling 204 8.4.4 Tagging 205 8.5 Summary 206 Chapter 9 Sharding 207 9.1 Sharding overview 208 9.1.1 What sharding is 208 9.1.2 How sharding works 210 9.2 A sample shard cluster 213 9.2.1 Setup 214 9.2.2 Writing to a sharded cluster 218 9.3 Querying and indexing a shard cluster 223 9.3.1 Shard query types 223 9.3.2 Indexing 227 9.4 Choosing a shard key 228 9.4.1 Ineffective shard keys 228 9.4.2 Ideal shard keys 230 9.5 Sharding in production 231 9.5.1 Deployment and configuration 231 9.5.2 Administration 235 9.6 Summary 240 Chapter 10 Deployment and administration 241 10.1 Deployment 242 10.1.1 Deployment environment 242 10.1.2 Server configuration 246 10.1.3 Data imports and exports 248 10.1.4 Security 249 10.2 Monitoring and diagnostics 251 10.2.1 Logging 251 10.2.2 Monitoring tools 252 10.2.3 External monitoring applications 255 10.2.4 Diagnostic tools (mongosniff, bsondump) 256 10.3 Maintenance 257 10.3.1 Backups and recovery 257 10.3.2 Compaction and repair 258 10.3.3 Upgrading 259 10.4 Performance troubleshooting 260 10.4.1 Check indexes and queries for efficiency 261 10.4.2 Add RAM 261 10.4.3 Increase disk performance 262 10.4.4 Scale horizontally 262 10.4.5 Seek professional assistance 263 10.5 Summary 263 Appendix A Installation 264 A.1 Installation 264 A.1.1 MongoDB on Linux 264 A.1.2 MongoDB on Mac OS X 265 A.1.3 MongoDB on Windows 267 A.1.4 Compiling MongoDB from source 268 A.1.5 Troubleshooting 268 A.2 Basic configuration options 269 A.3 Installing Ruby 270 A.3.1 Linux and Mac OS X 270 A.3.2 Windows 271 Appendix B Design patterns 272 B.1 Patterns 272 B.1.1 Embed versus reference 272 B.1.2 One-to-many 272 B.1.3 Many-to-many 273 B.1.4 Trees 274 B.1.5 Worker queues 277 B.1.6 Dynamic attributes 278 B.1.7 Transactions 279 B.1.8 Locality and precomputation 280 B.2 Anti-patterns 281 B.2.1 Careless indexing 281 B.2.2 Motley types 281 B.2.3 Bucket collections 281 B.2.4 Large, deeply nested documents 281 B.2.5 One collection per user 282 B.2.6 Unshardable collections 282 Appendix C Binary data and GridFS 283 C.1 Simple binary storage 283 C.1.1 Storing a thumbnail 283 C.1.2 Storing an MD5 284 C.2 GridFS 285 C.2.1 GridFS in Ruby 285 C.2.2 GridFS with mongofiles 288 Appendix D MongoDB in PHP, Java, and C++ 289 D.1 PHP 289 D.1.1 Documents 289 D.1.2 Connections 290 D.1.3 Sample program 290 D.2 Java 291 D.2.1 Documents 291 D.2.2 Connections 292 D.2.3 Sample program 292 D.3 C++ 293 D.3.1 Documents 293 D.3.2 Connections 295 D.3.3 Sample program 295 Appendix E Spatial indexing 297 E.1 Spatial indexing basics 297 E.2 Advanced queries 299 E.3 Compound spatial indexes 300 E.4 Spherical geometry 301 index 302 Symbols 302 Numerics 302 A 302 B 303 C 303 D 303 E 304 F 304 G 304 H 305 I 305 J 305 K 305 L 306 M 306 N 306 O 306 P 306 Q 307 R 307 S 308 T 309 U 309 V 309 W 310 X 310 Y 310 back cover 311