ebook img

MongoDB: The Definitive Guide: Powerful and Scalable Data Storage PDF

216 Pages·2010·4.105 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview MongoDB: The Definitive Guide: Powerful and Scalable Data Storage

www.it-ebooks.info www.it-ebooks.info MongoDB: The Definitive Guide www.it-ebooks.info www.it-ebooks.info MongoDB: The Definitive Guide Kristina Chodorow and Michael Dirolf Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo www.it-ebooks.info MongoDB: The Definitive Guide by Kristina Chodorow and Michael Dirolf Copyright © 2010 Kristina Chodorow and Michael Dirolf. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Julie Steele Indexer: Ellen Troutman Zaig Production Editor: Teresa Elsey Cover Designer: Karen Montgomery Copyeditor: Kim Wimpsett Interior Designer: David Futato Proofreader: Apostrophe Editing Services Illustrator: Robert Romano Production Services: Molly Sharp Printing History: September 2010: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. MongoDB: The Definitive Guide, the image of a mongoose lemur, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-38156-1 [M] 1283534198 www.it-ebooks.info Table of Contents Foreword ................................................................... xi Preface .................................................................... xiii 1. Introduction ........................................................... 1 A Rich Data Model 1 Easy Scaling 2 Tons of Features… 2 …Without Sacrificing Speed 3 Simple Administration 3 But Wait, That’s Not All… 4 2. Getting Started ......................................................... 5 Documents 5 Collections 7 Schema-Free 7 Naming 8 Databases 8 Getting and Starting MongoDB 10 MongoDB Shell 11 Running the Shell 11 A MongoDB Client 12 Basic Operations with the Shell 12 Tips for Using the Shell 14 Data Types 15 Basic Data Types 16 Numbers 18 Dates 19 Arrays 19 Embedded Documents 20 _id and ObjectIds 20 v www.it-ebooks.info 3. Creating, Updating, and Deleting Documents ............................... 23 Inserting and Saving Documents 23 Batch Insert 23 Inserts: Internals and Implications 24 Removing Documents 25 Remove Speed 25 Updating Documents 26 Document Replacement 26 Using Modifiers 27 Upserts 36 Updating Multiple Documents 38 Returning Updated Documents 39 The Fastest Write This Side of Mississippi 41 Safe Operations 42 Catching “Normal” Errors 43 Requests and Connections 43 4. Querying ............................................................. 45 Introduction to find 45 Specifying Which Keys to Return 46 Limitations 47 Query Criteria 47 Query Conditionals 47 OR Queries 48 $not 49 Rules for Conditionals 49 Type-Specific Queries 49 null 49 Regular Expressions 50 Querying Arrays 51 Querying on Embedded Documents 53 $where Queries 55 Cursors 56 Limits, Skips, and Sorts 57 Avoiding Large Skips 58 Advanced Query Options 60 Getting Consistent Results 61 Cursor Internals 63 5. Indexing ............................................................. 65 Introduction to Indexing 65 Scaling Indexes 68 Indexing Keys in Embedded Documents 68 vi | Table of Contents www.it-ebooks.info Indexing for Sorts 69 Uniquely Identifying Indexes 69 Unique Indexes 69 Dropping Duplicates 70 Compound Unique Indexes 70 Using explain and hint 70 Index Administration 75 Changing Indexes 76 Geospatial Indexing 77 Compound Geospatial Indexes 78 The Earth Is Not a 2D Plane 79 6. Aggregation .......................................................... 81 count 81 distinct 81 group 82 Using a Finalizer 84 Using a Function as a Key 86 MapReduce 86 Example 1: Finding All Keys in a Collection 87 Example 2: Categorizing Web Pages 89 MongoDB and MapReduce 90 7. Advanced Topics ....................................................... 93 Database Commands 93 How Commands Work 94 Command Reference 95 Capped Collections 97 Properties and Use Cases 98 Creating Capped Collections 99 Sorting Au Naturel 99 Tailable Cursors 101 GridFS: Storing Files 101 Getting Started with GridFS: mongofiles 102 Working with GridFS from the MongoDB Drivers 102 Under the Hood 103 Server-Side Scripting 104 db.eval 104 Stored JavaScript 105 Security 106 Database References 107 What Is a DBRef? 107 Example Schema 107 Table of Contents | vii www.it-ebooks.info Driver Support for DBRefs 108 When Should DBRefs Be Used? 108 8. Administration ....................................................... 111 Starting and Stopping MongoDB 111 Starting from the Command Line 112 File-Based Configuration 113 Stopping MongoDB 114 Monitoring 114 Using the Admin Interface 115 serverStatus 116 mongostat 118 Third-Party Plug-Ins 118 Security and Authentication 118 Authentication Basics 118 How Authentication Works 120 Other Security Considerations 121 Backup and Repair 121 Data File Backup 121 mongodump and mongorestore 122 fsync and Lock 123 Slave Backups 124 Repair 124 9. Replication .......................................................... 127 Master-Slave Replication 127 Options 128 Adding and Removing Sources 129 Replica Sets 130 Initializing a Set 132 Nodes in a Replica Set 133 Failover and Primary Election 135 Performing Operations on a Slave 136 Read Scaling 137 Using Slaves for Data Processing 137 How It Works 138 The Oplog 138 Syncing 139 Replication State and the Local Database 139 Blocking for Replication 140 Administration 141 Diagnostics 141 Changing the Oplog Size 141 viii | Table of Contents www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.