logo资料库

Manning.MongoDB.in.Action.2nd.Edition.2016.3.pdf

第1页 / 共482页
第2页 / 共482页
第3页 / 共482页
第4页 / 共482页
第5页 / 共482页
第6页 / 共482页
第7页 / 共482页
第8页 / 共482页
资料共482页,剩余部分请下载后查看
Front cover
brief contents
contents
preface
acknowledgments
about this book
How to use this book
Roadmap
Code conventions and downloads
Software requirements
Author Online
about the cover illustration
Part 1—Getting started
1 A database for the modern web
1.1 Built for the internet
1.2 MongoDB’s key features
1.2.1 Document data model
1.2.2 Ad hoc queries
1.2.3 Indexes
1.2.4 Replication
1.2.5 Speed and durability
1.2.6 Scaling
1.3 MongoDB’s core server and tools
1.3.1 Core server
1.3.2 JavaScript shell
1.3.3 Database drivers
1.3.4 Command-line tools
1.4 Why MongoDB?
1.4.1 MongoDB versus other databases
1.4.2 Use cases and production deployments
1.5 Tips and limitations
1.6 History of MongoDB
1.7 Additional resources
1.8 Summary
2 MongoDB through the JavaScript shell
2.1 Diving into the MongoDB shell
2.1.1 Starting the shell
2.1.2 Databases, collections, and documents
2.1.3 Inserts and queries
2.1.4 Updating documents
2.1.5 Deleting data
2.1.6 Other shell features
2.2 Creating and querying with indexes
2.2.1 Creating a large collection
2.2.2 Indexing and explain( )
2.3 Basic administration
2.3.1 Getting database information
2.3.2 How commands work
2.4 Getting help
2.5 Summary
3 Writing programs using MongoDB
3.1 MongoDB through the Ruby lens
3.1.1 Installing and connecting
3.1.2 Inserting documents in Ruby
3.1.3 Queries and cursors
3.1.4 Updates and deletes
3.1.5 Database commands
3.2 How the drivers work
3.2.1 Object ID generation
3.3 Building a simple application
3.3.1 Setting up
3.3.2 Gathering data
3.3.3 Viewing the archive
3.4 Summary
Part 2—Application development in MongoDB
4 Document-oriented data
4.1 Principles of schema design
4.2 Designing an e-commerce data model
4.2.1 Schema basics
4.2.2 Users and orders
4.2.3 Reviews
4.3 Nuts and bolts: On databases, collections, and documents
4.3.1 Databases
4.3.2 Collections
4.3.3 Documents and insertion
4.4 Summary
5 Constructing queries
5.1 E-commerce queries
5.1.1 Products, categories, and reviews
5.1.2 Users and orders
5.2 MongoDB’s query language
5.2.1 Query criteria and selectors
5.2.2 Query options
5.3 Summary
6 Aggregation
6.1 Aggregation framework overview
6.2 E-commerce aggregation example
6.2.1 Products, categories, and reviews
6.2.2 User and order
6.3 Aggregation pipeline operators
6.3.1 $project
6.3.2 $group
6.3.3 $match, $sort, $skip, $limit
6.3.4 $unwind
6.3.5 $out
6.4 Reshaping documents
6.4.1 String functions
6.4.2 Arithmetic functions
6.4.3 Date functions
6.4.4 Logical functions
6.4.5 Set Operators
6.4.6 Miscellaneous functions
6.5 Understanding aggregation pipeline performance
6.5.1 Aggregation pipeline options
6.5.2 The aggregation framework’s explain( ) function
6.5.3 allowDiskUse option
6.5.4 Aggregation cursor option
6.6 Other aggregation capabilities
6.6.1 .count( ) and .distinct( )
6.6.2 map-reduce
6.7 Summary
7 Updates, atomic operations, and deletes
7.1 A brief tour of document updates
7.1.1 Modify by replacement
7.1.2 Modify by operator
7.1.3 Both methods compared
7.1.4 Deciding: replacement vs. operators
7.2 E-commerce updates
7.2.1 Products and categories
7.2.2 Reviews
7.2.3 Orders
7.3 Atomic document processing
7.3.1 Order state transitions
7.3.2 Inventory management
7.4 Nuts and bolts: MongoDB updates and deletes
7.4.1 Update types and options
7.4.2 Update operators
7.4.3 The findAndModify command
7.4.4 Deletes
7.4.5 Concurrency, atomicity, and isolation
7.4.6 Update performance notes
7.5 Reviewing update operators
7.6 Summary
Part 3—MongoDB mastery
8 Indexing and query optimization
8.1 Indexing theory
8.1.1 A thought experiment
8.1.2 Core indexing concepts
8.1.3 B-trees
8.2 Indexing in practice
8.2.1 Index types
8.2.2 Index administration
8.3 Query optimization
8.3.1 Identifying slow queries
8.3.2 Examining slow queries
8.3.3 Query patterns
8.4 Summary
9 Text search
9.1 Text searches—not just pattern matching
9.1.1 Text searches vs. pattern matching
9.1.2 Text searches vs. web page searches
9.1.3 MongoDB text search vs. dedicated text search engines
9.2 Manning book catalog data download
9.3 Defining text search indexes
9.3.1 Text index size
9.3.2 Assigning an index name and indexing all text fields in a collection
9.4 Basic text search
9.4.1 More complex searches
9.4.2 Text search scores
9.4.3 Sorting results by text search score
9.5 Aggregation framework text search
9.5.1 Where’s MongoDB in Action, Second Edition?
9.6 Text search languages
9.6.1 Specifying language in the index
9.6.2 Specifying the language in the document
9.6.3 Specifying the language in a search
9.6.4 Available languages
9.7 Summary
10 WiredTiger and pluggable storage
10.1 Pluggable Storage Engine API
10.1.1 Why use different storages engines?
10.2 WiredTiger
10.2.1 Switching to WiredTiger
10.2.2 Migrating your database to WiredTiger
10.3 Comparison with MMAPv1
10.3.1 Configuration files
10.3.2 Insertion script and benchmark script
10.3.3 Insertion benchmark results
10.3.4 Read performance scripts
10.3.5 Read performance results
10.3.6 Benchmark conclusion
10.4 Other examples of pluggable storage engines
10.5 Advanced topics
10.5.1 How does a pluggable storage engine work?
10.5.2 Data structure
10.5.3 Locking
10.6 Summary
11 Replication
11.1 Replication overview
11.1.1 Why replication matters
11.1.2 Replication use cases and limitations
11.2 Replica sets
11.2.1 Setup
11.2.2 How replication works
11.2.3 Administration
11.3 Drivers and replication
11.3.1 Connections and failover
11.3.2 Write concern
11.3.3 Read scaling
11.3.4 Tagging
11.4 Summary
12 Scaling your system with sharding
12.1 Sharding overview
12.1.1 What is sharding?
12.1.2 When should you shard?
12.2 Understanding components of a sharded cluster
12.2.1 Shards: storage of application data
12.2.2 Mongos router: router of operations
12.2.3 Config servers: storage of metadata
12.3 Distributing data in a sharded cluster
12.3.1 Ways data can be distributed in a sharded cluster
12.3.2 Distributing databases to shards
12.3.3 Sharding within collections
12.4 Building a sample shard cluster
12.4.1 Starting the mongod and mongos servers
12.4.2 Configuring the cluster
12.4.3 Sharding collections
12.4.4 Writing to a sharded cluster
12.5 Querying and indexing a shard cluster
12.5.1 Query routing
12.5.2 Indexing in a sharded cluster
12.5.3 The explain() tool in a sharded cluster
12.5.4 Aggregation in a sharded cluster
12.6 Choosing a shard key
12.6.1 Imbalanced writes (hotspots)
12.6.2 Unsplittable chunks (coarse granularity)
12.6.3 Poor targeting (shard key not present in queries)
12.6.4 Ideal shard keys
12.6.5 Inherent design trade-offs (email application)
12.7 Sharding in production
12.7.1 Provisioning
12.7.2 Deployment
12.7.3 Maintenance
12.8 Summary
13 Deployment and administration
13.1 Hardware and provisioning
13.1.1 Cluster topology
13.1.2 Deployment environment
13.1.3 Provisioning
13.2 Monitoring and diagnostics
13.2.1 Logging
13.2.2 MongoDB diagnostic commands
13.2.3 MongoDB diagnostic tools
13.2.4 MongoDB Monitoring Service
13.2.5 External monitoring applications
13.3 Backups
13.3.1 mongodump and mongorestore
13.3.2 Data file–based backups
13.3.3 MMS backups
13.4 Security
13.4.1 Secure environments
13.4.2 Network encryption
13.4.3 Authentication
13.4.4 Replica set authentication
13.4.5 Sharding authentication
13.4.6 Enterprise security features
13.5 Administrative tasks
13.5.1 Data imports and exports
13.5.2 Compaction and repair
13.5.3 Upgrading
13.6 Performance troubleshooting
13.6.1 Working set
13.6.2 Performance cliff
13.6.3 Query interactions
13.6.4 Seek professional assistance
13.7 Deployment checklist
13.8 Summary
Appendix A—Installation
A.1 Installation
A.1.1 Production deployments
A.1.2 32-bit vs. 64-bit
A.2 MongoDB on Linux
A.2.1 Installing with precompiled binaries
A.2.2 Using a package manager
A.3 MongoDB on Mac OS X
A.3.1 Precompiled binaries
A.3.2 Using a package manager
A.4 MongoDB on Windows
A.4.1 Precompiled binaries
A.5 Compiling MongoDB from source
A.6 Troubleshooting
A.6.1 Wrong architecture
A.6.2 Nonexistent data directory
A.6.3 Lack of permissions
A.6.4 Unable to bind to port
A.7 Basic configuration options
A.8 Installing Ruby
A.8.1 Linux and Mac OS X
A.8.2 Windows
Appendix B—Design patterns
B.1 Embed vs. reference
B.2 One-to-many
B.3 Many-to-many
B.4 Trees
B.5 Worker queues
B.6 Dynamic attributes
B.7 Transactions
B.8 Locality and precomputation
B.9 Antipatterns
B.9.1 Careless indexing
B.9.2 Motley types
B.9.3 Bucket collections
B.9.4 Large, deeply nested documents
B.9.5 One collection per user
B.9.6 Unshardable collections
Appendix C—Binary data and GridFS
C.1 Simple binary storage
C.1.1 Storing a thumbnail
C.1.2 Storing an MD5
C.2 GridFS
C.2.1 GridFS in Ruby
C.2.2 GridFS with mongofiles
index
Symbols
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Back cover
IN ACTION SECOND EDITION Kyle Banker Peter Bakkum Shaun Verch Douglas Garrett Tim Hawkins M A N N I N G Covers MongoDB version 3.0
MongoDB in Action
MongoDB in Action Second Edition KYLE BANKER PETER BAKKUM SHAUN VERCH DOUGLAS GARRETT TIM HAWKINS M A N N I N G SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: 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, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the 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 books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editors: Susan Conant, Jeff Bleiel Technical development editors: Brian Hanafee, Jürgen Hoffman, Wouter Thielen Copyeditors: Liz Welch, Jodie Allen Proofreader: Melody Dolab Technical proofreader: Doug Warren Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617291609 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
This book is dedicated to peace and human dignity and to all those who work for these ideals
brief contents PART 1 GETTING STARTED . ......................................................1 1 ■ A database for the modern web 3 2 ■ MongoDB through the JavaScript shell 29 3 ■ Writing programs using MongoDB 52 PART 2 APPLICATION DEVELOPMENT IN MONGODB.................71 4 ■ Document-oriented data 73 5 ■ Constructing queries 98 6 ■ Aggregation 120 7 ■ Updates, atomic operations, and deletes 157 PART 3 MONGODB MASTERY.................................................195 Indexing and query optimization 197 8 ■ 9 ■ Text search 244 10 ■ WiredTiger and pluggable storage 273 11 ■ Replication 296 12 ■ Scaling your system with sharding 333 13 ■ Deployment and administration 376 vii
分享到:
收藏