MongoDB In Action - Apphosting.io

Transcription

IN ACTIONSECOND EDITIONKyle BankerPeter BakkumShaun VerchDouglas GarrettTim HawkinsMANNINGCovers MongoDB version 3.0www.allitebooks.comwww.ebook3000.com

MongoDB in Actionwww.allitebooks.com

www.allitebooks.comwww.ebook3000.com

MongoDB in ActionSecond EditionKYLE BANKERPETER BAKKUMSHAUN VERCHDOUGLAS GARRETTTIM HAWKINSMANNINGSHELTER ISLANDwww.allitebooks.com

For online information and ordering of this and other Manning books, please visitwww.manning.com. The publisher offers discounts on this book when ordered in quantity.For more information, please contactSpecial Sales DepartmentManning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964Email: orders@manning.com 2016 by Manning Publications Co. All rights reserved.No part of this publication may be reproduced, stored in a retrieval system, or transmitted, inany form or by means electronic, mechanical, photocopying, or otherwise, without prior writtenpermission of the publisher.Many of the designations used by manufacturers and sellers to distinguish their products areclaimed as trademarks. Where those designations appear in the book, and ManningPublications was aware of a trademark claim, the designations have been printed in initial capsor all caps.Recognizing the importance of preserving what has been written, it is Manning’s policy to havethe books we publish printed on acid-free paper, and we exert our best efforts to that end.Recognizing also our responsibility to conserve the resources of our planet, Manning booksare printed on paper that is at least 15 percent recycled and processed without the use ofelemental chlorine.Manning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964Development editors: Susan Conant, Jeff BleielTechnical development editors: Brian Hanafee, Jürgen Hoffman,Wouter ThielenCopyeditors: Liz Welch, Jodie AllenProofreader: Melody DolabTechnical proofreader: Doug WarrenTypesetter: Dennis DalinnikCover designer: Marija TudorISBN: 9781617291609Printed in the United States of America1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16www.allitebooks.comwww.ebook3000.com

This book is dedicated to peace and human dignityand to all those who work for these idealswww.allitebooks.com

www.allitebooks.comwww.ebook3000.com

brief contentsPART 1PART 2PART 3GETTING STARTED . .11 A database for the modern web32 MongoDB through the JavaScript shell 293 Writing programs using MongoDB 52APPLICATION DEVELOPMENT IN MONGODB.714 Document-oriented data735 Constructing queries 986 Aggregation 1207 Updates, atomic operations, and deletes157MONGODB MASTERY .1958 Indexing and query optimization 1979 Text search 24410 WiredTiger and pluggable storage11 Replication 29612 Scaling your system with sharding13 Deployment and administration 376viiwww.allitebooks.com273333

www.allitebooks.comwww.ebook3000.com

contentspreface xviiacknowledgments xixabout this book xxiabout the cover illustrationxxivPART 1 GETTING STARTED . .11A database for the modern web 31.11.2Built for the internet 5MongoDB’s key features 6Document data model 6 Ad hoc queries 10Indexes 10 Replication 11 Speed and durability 12Scaling 14 1.3 MongoDB’s core server and toolsCore server 16 JavaScript shellCommand-line tools 18 1.4Why MongoDB?16 Database drivers18MongoDB versus other databases 19production deployments 221.51.615Tips and limitations 24History of MongoDB 25ixwww.allitebooks.com Use cases and17

CONTENTSx1.71.82Additional resourcesSummary 2827MongoDB through the JavaScript shell 292.1Diving into the MongoDB shell 30Starting the shell 30 Databases, collections, and documentsInserts and queries 32 Updating documents 34Deleting data 38 Other shell features 38 31 2.2Creating and querying with indexes 39Creating a large collection2.3Basic administration393Indexing and explain( )4146Getting database information2.42.5 46 How commands work 48Getting help 49Summary 51Writing programs using MongoDB 523.1MongoDB through the Ruby lens 53Installing and connectingQueries and cursors 56Database commands 583.2How the drivers workObject ID generation3.35959Building a simple applicationSetting up 613.453 Inserting documents in Ruby 55Updates and deletes 57 61Gathering data 62 Viewing the archive65Summary 69PART 2 APPLICATION DEVELOPMENT IN MONGODB .714Document-oriented data 734.14.2Principles of schema design 74Designing an e-commerce data modelSchema basics 764.3Users and orders 8075 Reviews 83Nuts and bolts: On databases, collections,and documents 84Databases 844.4 Collections 87 Summary 96www.ebook3000.comDocuments and insertion 92

CONTENTS5xiConstructing queries 985.1E-commerce queries99Products, categories, and reviews5.2MongoDB’s query languageQuery criteria and selectors5.3699 Users and orders101103103 Query options 117Summary 119Aggregation 1206.16.2Aggregation framework overview 121E-commerce aggregation example 123Products, categories, and reviews 125User and order 1326.3Aggregation pipeline operators 135 project 136 group 136 match, sort, skip, limit 138 unwind 139 out 139 6.4 Reshaping documents140String functions 141 Arithmetic functions 142Date functions 142 Logical functions 143Set Operators 144 Miscellaneous functions 145 6.5Understanding aggregation pipeline performance 146Aggregation pipeline options 147 The aggregation framework’sexplain( ) function 147 allowDiskUse option 151Aggregation cursor option 151 6.6Other aggregation capabilities.count( ) and .distinct( )6.77153 152map-reduce 153Summary 156Updates, atomic operations, and deletes 1577.1A brief tour of document updates 158Modify by replacement 159 Modify by operator 159Both methods compared 160 Deciding: replacementvs. operators 160 7.2E-commerce updates 162Products and categories7.3162Reviews 167 Atomic document processingOrder state transitions172 Orders168171Inventory management 174

CONTENTSxii7.4Nuts and bolts: MongoDB updates and deletes179Update types and options 179 Update operators 181The findAndModify command 188 Deletes 189Concurrency, atomicity, and isolation 190Update performance notes 191 7.57.6Reviewing update operatorsSummary 193192PART 3 MONGODB MASTERY .1958Indexing and query optimization 1978.1Indexing theory 198A thought experiment 198B-trees 2058.2Indexing in practiceIndex types8.32079201207Index administration 211 Query optimization216Identifying slow queriesQuery patterns 2418.4Core indexing concepts 217 Examining slow queries221Summary 243Text search 2449.1Text searches—not just pattern matching 245Text searches vs. pattern matching 246 Text searches vs.web page searches 247 MongoDB text search vs. dedicatedtext search engines 250 9.29.3Manning book catalog data downloadDefining text search indexes 255253Text index size 255 Assigning an index name and indexingall text fields in a collection 256 9.4Basic text search257More complex searches 259 Text search scoresSorting results by text search score 262 9.5261Aggregation framework text search 263Where’s MongoDB in Action, Second Edition?www.ebook3000.com265

CONTENTS9.6Text search languagesxiii267Specifying language in the index 267 Specifying the language inthe document 269 Specifying the language in a search 269Available languages 271 9.710Summary 272WiredTiger and pluggable storage 27310.110.2Pluggable Storage Engine API273Why use different storages engines?274WiredTiger275Switching to WiredTigerto WiredTiger 27710.3276Migrating your database Comparison with MMAPv1278Configuration files 279 Insertion script andbenchmark script 281 Insertion benchmark results 283Read performance scripts 285 Read performance results 286Benchmark conclusion 288 10.410.5Other examples of pluggable storage enginesAdvanced topics 290How does a pluggable storage engine work?Data structure 292 Locking 294290 10.611SummaryReplication11.1295296Replication overview297Why replication mattersand limitations 29811.2Replica sets297 Replication use cases300Setup 300 How replication worksAdministration 314 11.3Drivers and replication307324Connections and failover 324 Write concernRead scaling 328 Tagging 330 11.4Summary332327289

CONTENTSxiv12Scaling your system with sharding 33312.1Sharding overview334What is sharding? 33412.2 When should you shard? 335Understanding components of a sharded cluster 336Shards: storage of application data 337 Mongos router: routerof operations 338 Config servers: storage of metadata 338 12.3Distributing data in a sharded cluster339Ways data can be distributed in a sharded cluster 340Distributing databases to shards 341 Sharding withincollections 341 12.4Building a sample shard cluster343Starting the mongod and mongos servers 343the cluster 346 Sharding collections 347sharded cluster 349 12.5Querying and indexing a shard cluster ConfiguringWriting to a355Query routing 355 Indexing in a sharded cluster 356The explain() tool in a sharded cluster 357 Aggregation ina sharded cluster 359 12.6Choosing a shard key 359Imbalanced writes (hotspots) 360 Unsplittable chunks (coarsegranularity) 362 Poor targeting (shard key not presentin queries) 362 Ideal shard keys 363 Inherent designtrade-offs (email application) 364 12.7Sharding in productionProvisioning 36612.813 Summary 365Deployment369 Maintenance 370375Deployment and administration 37613.1Hardware and provisioningCluster topology 377Provisioning 38513.2 377Deployment environment 378Monitoring and diagnostics386Logging 387 MongoDB diagnostic commands 387MongoDB diagnostic tools 388 MongoDB MonitoringService 390 External monitoring applications 390 13.3Backups 391mongodump and mongorestore 391 Data file–basedbackups 392 MMS backups 393 www.ebook3000.com

CONTENTS13.4xvSecurity 394Secure environments 394 Network encryption 395Authentication 397 Replica set authentication 401Sharding authentication 402 Enterprise security features 402 13.5Administrative tasks402Data imports and exportsUpgrading 40513.6402 Performance troubleshootingWorking set 406Query interactions Deployment checklistSummary 410appendix Aappendix Bappendix CInstallation 411Design patterns 421Binary data and GridFS 408433403405Performance cliff 407407 Seek professional assistance13.713.8index 441Compaction and repair408

www.ebook3000.com

prefaceDatabases are the workhorses of the information age. Like Atlas, they go largely unnoticed in supporting the digital world we’ve come to inhabit. It’s easy to forget that ourdigital interactions, from commenting and tweeting to searching and sorting, are inessence interactions with a database. Because of this fundamental yet hidden function, I always experience a certain sense of awe when thinking about databases, notunlike the awe one might feel when walking across a suspension bridge normallyreserved for automobiles.The database has taken many forms. The indexes of books and the card catalogsthat once stood in libraries are both databases of a sort, as are the ad hoc structuredtext files of the Perl programmers of yore. Perhaps most recognizable now as databases proper are the sophisticated, fortune-making relational databases that underliemuch of the world’s software. These relational databases, with their idealized thirdnormal forms and expressive SQL interfaces, still command the respect of the oldguard, and appropriately so.But as a working web application developer a few years back, I was eager to samplethe emerging alternatives to the reigning relational database. When I discoveredMongoDB, the resonance was immediate. I liked the idea of using a JSON-like structure to represent data. JSON is simple, intuitive, and human-friendly. That MongoDBalso based its query language on JSON lent a high degree of comfort and harmony tothe usage of this new database. The interface came first. Compelling features like easyreplication and sharding made the package all the more intriguing. And by the timexvii

xviiiPREFACEI’d built a few applications on MongoDB and beheld the ease of development itimparted, I’d become a convert.Through an unlikely turn of events, I started working for 10gen, the companyspearheading the development of this open source database. For two years, I’ve hadthe opportunity to improve various client drivers and work with numerous customerson their MongoDB deployments. The experience gained through this process has, Ihope, been distilled faithfully into the book you’re reading now.As a piece of software and a work in progress, MongoDB is still far from perfection.But it’s also successfully supporting thousands of applications atop database clusterssmall and large, and it’s maturing daily. It’s been known to bring out wonder, evenhappiness, in many a developer. My hope is that it can do the same for you.This is the second edition of MongoDB in Action and I hope that you enjoy reading the book!KYLE BANKERwww.ebook3000.com

acknowledgmentsThanks are due to folks at Manning for helping make this book a reality. MichaelStephens helped conceive the first edition of this book, and my development editorsfor this second edition, Susan Conant, Jeff Bleiel, and Maureen Spencer, pushed thebook to completion while being helpful along the way. My thanks go to them.Book writing is a time-consuming enterprise. I feel I wouldn’t have found the timeto finish this book had it not been for the generosity of Eliot Horowitz and DwightMerriman. Eliot and Dwight, through their initiative and ingenuity, created MongoDB,and they trusted me to document the project. My thanks to them.Many of the ideas in this book owe their origins to conversations I had with colleagues at 10gen. In this regard, special thanks are due to Mike Dirolf, Scott Hernandez,Alvin Richards, and Mathias Stearn. I’m especially indebted to Kristina Chowdorow,Richard Kreuter, and Aaron Staple for providing expert reviews of entire chapters forthe first edition.The following reviewers read the manuscript of the first edition at various stagesduring its development: Kevin Jackson, Hardy Ferentschik, David Sinclair, ChrisChandler, John Nunemaker, Robert Hanson, Alberto Lerner, Rick Wagner, Ryan Cox,Andy Brudtkuhl, Daniel Bretoi, Greg Donald, Sean Reilly, Curtis Miller, SanchetDighe, Philip Hallstrom, and Andy Dingley. And I am also indebted to all the reviewers who read the second edition, including Agustin Treceno, Basheeruddin Ahmed,Gavin Whyte, George Girton, Gregor Zurowski, Hardy Ferentschik, Hernan Garcia,Jeet Marwah, Johan Mattisson, Jonathan Thoms, Julia Varigina, Jürgen Hoffmann,Mike Frey, Phlippie Smith, Scott Lyons, and Steve Johnson. Special thanks go to WouterThielen for his work on chapter 10, technical editor Mihalis Tsoukalos, who devotedxixwww.allitebooks.com

xxACKNOWLEDGMENTSmany hours to whipping the second edition into shape, and to Doug Warren for histhorough technical review of the second edition shortly before it went to press.My amazing wife, Dominika, offered her patience and support, through the writingof both editions of this book, and to my wonderful son, Oliver, just for being awesome.KYLE BANKERwww.ebook3000.com

about this bookThis book is for application developers and DBAs wanting to learn MongoDB from theground up. If you’re new to MongoDB, you’ll find in this book a tutorial that moves ata comfortable pace. If you’re already a user, the more detailed reference sections inthe book will come in handy and should fill any gaps in your knowledge. In terms ofdepth, the material should be suitable for all but the most advanced users. Althoughthe book is about the latest MongoDB version, which at the time of writing is 3.0.x, italso covers the previous stable MongoDB version that is 2.6.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 simplebut 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 accessibleway possible. If you have experience with other programming languages, you shouldfind the examples easy to follow.One more note about languages. If you’re wondering, “Why couldn’t this book uselanguage X?” you can take heart. The officially supported MongoDB drivers featureconsistent and analogous APIs. This means that once you learn the basic API for onedriver, you can pick up the others fairly easily.How to use this bookThis 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’llxxi

ABOUT THIS BOOKxxiineed 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 particulartopics. Chapters 8 to 13 and all of the appendixes stand on their own and can safely beread in any order. Additionally, chapters 4 to 7 contain the so-called “nuts and bolts”sections, which focus on fundamentals. These also can be read outside the flow of thesurrounding text.RoadmapThis book is divided into three parts.Part 1 is an end-to-end introduction to MongoDB. Chapter 1 gives an overview ofMongoDB’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 throughthe design of a simple application that uses MongoDB on the back end.Part 2 is an elaboration on the MongoDB API presented in part 1. With a specificfocus on application development, the four chapters in part 2 progressively describe aschema 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 schemadesign. Chapters 5, 6, and 7 then teach you how to work with this schema by coveringqueries 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 MongoDB mastery. Chapter 8 is a thorough study of indexingand query optimization. The subject of Chapter 9 is text searching inside MongoDB.Chapter 10, which is totally new in this edition, is about the WiredTiger storage engineand pluggable storage, which are unique features of MongoDB v3. Chapter 11 concentrates on replication, with strategies for deploying MongoDB for high availability andread scaling. Chapter 12 describes sharding, MongoDB’s path to horizontal scalability.And chapter 13 provides a series of best practices for deploying, administering, andtroubleshooting MongoDB installations.The book ends with three appendixes. Appendix A covers installation of MongoDBand Ruby (for the driver examples) on Linux, Mac OS X, and Windows. Appendix Bpresents a series of schema and application design patterns, and it also includes a listof anti-patterns. Appendix C shows how to work with binary data in MongoDB andhow to use GridFS, a spec implemented by all the drivers, to store especially large filesin the database.Code conventions and downloadsAll 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.www.ebook3000.com

ABOUT THIS BOOKxxiiiAs 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 seereferences to bug reports and planned improvements. For example, the ticket foradding full-text search to the database is SERVER-380. To view the status of any suchticket, point your browser to http://jira.mongodb.org, and enter the ticket ID in thesearch box.You can download the book’s source code, with some sample data, from the book’ssite at http://mongodb-book.com as well as from the publisher’s website at http://manning.com/MongoDBinAction.Software requirementsTo 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 theofficial 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 OnlineThe purchase of MongoDB in Action, Second Edition includes free access to a privateforum 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 accessand 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 meaningfuldialogue between individual readers and between readers and the author can takeplace. It’s not a commitment to any specific amount of participation on the part of theauthor, whose contribution to the book’s forum remains voluntary (and unpaid). Wesuggest 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.

about the cover illustrationThe figure on the cover of MongoDB in Action is captioned “Le Bourginion,” or a resident of the Burgundy region in northeastern France. The illustration is taken from anineteenth-century collection of works by many artists, edited by Louis Curmer andpublished in Paris in 1841. The title of the collection is Les Français peints par euxmêmes, which translates as The French People Painted by Themselves. Each illustration isfinely drawn and colored by hand, and the rich variety of drawings in the collectionreminds us vividly of how culturally apart the world’s regions, towns, villages, andneighborhoods were just 200 years ago. Isolated from each other, people spoke different dialects and languages. In the streets or in the countryside, it was easy to identifywhere they lived and what their trade or station in life was just by their dress.Dress codes have changed since then and the diversity by region, so rich at thetime, has faded away. It is now hard to tell apart the inhabitants of different continents, let alone different towns or regions. Perhaps we have traded cultural diversityfor a more varied personal life—certainly for a more varied and fast-paced technological life.At a time when it is hard to tell one computer book from another, Manning celebrates the inventiveness and initiative of the computer business with book coversbased on the rich diversity of regional life of two centuries ago, brought back to life bypictures from collections such as this one.xxivwww.ebook3000.com

Part 1Getting startedPart 1 of this book provides a broad, practical introduction to MongoDB. Italso introduces the JavaScript shell and the Ruby driver, both of which are usedin examples throughout the book.We’ve written this book with developers in mind, but it should be useful evenif you’re a casual user of MongoDB. Some programming experience will provehelpful in understanding the examples, though we focus most on MongoDBitself. If you’ve worked with relational databases in the past, great! We comparethese to MongoDB often.MongoDB version 3.0.x is the most recent MongoDB version at the time ofwriting, but most of the discussion applies to previous versions of MongoDB(and presumably later versions). We usually mention it when a particular featurewasn’t available in previous versions.You’ll use JavaScript for most examples because MongoDB’s JavaScript shellmakes it easy for you to experiment with these queries. Ruby is a popular language among MongoDB users, and our examples show how the use of Ruby inreal-world applications can take advantage of MongoDB. Rest assured, even ifyou’re not a Ruby developer you can access MongoDB in much the same way asin other languages.In chapter 1, you’ll look at MongoDB’s history, design goals, and applicationuse cases. You’ll also see what makes MongoDB unique as you compare it withother databases emerging in the “NoSQL” space.In chapter 2, you’ll become conversant in the language of MongoDB’s shell.You’ll learn the basics of MongoDB’s query language, and you’ll practice by

2PART 1Getting startedcreating, querying, updating, and deleting documents. The chapter also features someadvanced shell tricks and MongoDB commands.Chapter 3 introduces the MongoDB drivers and MongoDB’s data format, BSON.Here you’ll learn how to talk to the database through the Ruby programming language, and you’ll build a simple application in Ruby demonstrating MongoDB’s flexibility and query power.To get the most out of this book, follow along and try out the examples. If you don’thave MongoDB installed yet, appendix A can help you get it running on your machine.www.ebook3000.com

A databasefor the modern webThis chapter covers MongoDB’s history, design goals, and keyfeatures A brief introduction to the shell and drivers Use cases and limitations Recent changes in MongoDBIf you’ve built web applications in recent years, you’ve probably used a relationaldatabase as the primary data store. If you’re familiar with SQL, you might appreciate the usefulness of a well-normalized1 data model, the necessity of transactions,and the assurances provided by a durable storage engine. Simply put, the relationaldatabase is mature and well-known. When developers start advocating alternativedatastores, questions about the viability and utility of these new technologiesarise. Are these new datastores replacements for relational database systems?Who’s using them in production, and why? What trade-offs are involved in moving1When we mention normalization we’re usually talking about reducing redundancy when you store data.For example, in a SQL database you can split parts of your data, such as users and orders, into their owntables to reduce redundant storage of usernames.3

4CHAPTER 1 A database for the modern webto a nonrelational database? The answers to those questions rest on the answer to thisone: why are developers interested in MongoDB?MongoDB is a database management system designed to rapidly develop web applications and internet infrastructure. The data model and persistence strategies arebuilt for high read-and-write throughput and the ability to scale easily with automaticfailover. Whether an application requires just one database node or dozens of them,MongoDB can provide surprisingly good performance. If you’ve experienced difficulties scaling relational databases, this may be great news. But not everyone needs tooperate at scale. Maybe all you’ve ever needed is a single database server. Why wouldyou use MongoDB?Perhaps the biggest reason developers use MongoDB isn’t because of its scalingstrategy, but because of its intuitive data model. MongoDB stores its information indocuments rather than rows. What’s a document? Here’s an example:{id: 10,username: 'peter',email: 'pbbakkum@gmail.com'}This is a pretty simple document; it’s storing a few fields of information about a user(he sounds cool). What’s the advantage of this model? Consider the case where you’dlike to store multiple emails for each user. In the relational world, you might create aseparate table of email addresses and the users to which they’re associated. MongoDBgives you another way to store these:{id: 10,username: 'peter',email: ['pbbakkum@gmail.com','pbb7c@virginia.edu']}And just like that, you’ve created an array of email addresses and solved your problem.As a developer, you’ll find it extremely useful to be able to store a structured document like this in your database without worrying about fitting a schema or addingmore tables when your data changes.MongoDB’s document format is based on JSON, a popular scheme for storing arbitrary data structures. JSON is an acronym for JavaScript Object Notation. As you just saw,JSON structures consist of keys and values, and they can nest arbitrarily deep. They’reanalogous to the dictionaries and hash maps of other programming languages.A document-based data model can represent rich, hierarchical data structures. It’soften possible to do without the multitable joins common to relational databases.For example, suppose you’re modeling products for an e-commerce site. With a fullywww.ebook3000.com

Built for the internet5normalized relational data model, the information for any one product might bedivided among dozens of tables. If you want to get a product representation from thedatabase shell, you’ll need to write a SQL query full of joins.With a document model, by contrast, most of a product’s information can be represented within a single document. When you open the MongoDB JavaScript shell,you can easily get a comprehensible representation of your product with all its information hierarchically organized in a JSON-like structure. You can also query for it andmanipulate it. MongoDB’s query capabilities are designed specifically for manipulating structured documents, so users switching from relational databases experience asimilar level of query power. In addition, most developers now work with object-orientedlanguages, and they want a data store that better maps to objects. With MongoDB, anobject defined in the programming language can often be persisted as is, removingsome of the complexity of object mappers. If you’re experienced with relational databases, it can be helpful to approach MongoDB from the perspective of transitioningyour existing skills into this new databas

2 MongoDB through the JavaScript shell 29 2.1 Diving into the MongoDB shell 30 Starting the shell 30 Databases, collections, and documents 31 Inserts and queries 32 Updating documents 34 Deleting data 38 Other shell features 38 2.2 Creating and querying with indexes 39 Creating a large collection 39 Indexing and explain( ) 41