• No results found

MongoDB. The Definitive Guide to. The NoSQL Database for Cloud and Desktop Computing. Apress8. Eelco Plugge, Peter Membrey and Tim Hawkins

N/A
N/A
Protected

Academic year: 2021

Share "MongoDB. The Definitive Guide to. The NoSQL Database for Cloud and Desktop Computing. Apress8. Eelco Plugge, Peter Membrey and Tim Hawkins"

Copied!
12
0
0

Loading.... (view fulltext now)

Full text

(1)

MongoDB

The

NoSQL

Database for

Cloud

and

Desktop Computing

TECHNISCHE

INFORMATIONSBIBLIO

1HEK

UNIVERSITATSBIBLIOTHEK

HANNOVER

11 111

Eelco

Plugge,

Peter

Membrey

and

Tim

Hawkins

Apress8

TIB/UB Hannover 89 133 302 342

(2)

EContentsata Glance iv

About the Authors xvi About theTechnical Reviewer xvii

Acknowledgments

xviii

Introduction xx

Part I: Basics 1

Chapter

1: Introduction to

MongoDB

3

Reviewing

the

MongoDB Philosophy

3

UsingtheRightToolfor theRightJob 3 LackingInnateSupportfor Transactions 5 DrillingDown onJSON and How It RelatestoMongoDB 5

AdoptingaNon-RelationalApproach 7

Optingfor Performancevs.Features 8

Runningthe DatabaseAnywhere 9

Fitting Everything Together

9

GeneratingorCreating aKey 9

Using Keysand Values 10

Implementing Collections 11

UnderstandingDatabases 11

Reviewing

the Feature List 11

UsingDocument-OrientatedStorage (BSON) 11

Supporting DynamicQueries 12

(3)

IndexingYourDocuments 13

Leveraging GeospatialIndexes • • ••••• • 13

ProfilingQueries •** I4

UpdatingInformation In-Place • .14

Storing BinaryData • • 14

Replicating Data 15

implementingAutoSharding • • -""IS

Using Mapand Reduce Functions 16

Getting

Help

»»• > »• »• »«16

Visitingthe Website - 16

Chatting with theMongoDBDevelopers , 16

CuttingandPasting MongoDB Code ...17

FindingSolutionson GoogleGroups 17

LeveragingtheJIRATracking System 17

Summary

17

Chapter

2:

Installing

MongoDB

... 19

Choosing

YourVersion < 19

Understandingthe Version Numbers , 20

Installing MongoDB

onYour

System,...,

...20 Installing MongoDBUnder Linux 20

Installing MongoDBUnder Windows 22

Running

MongoDB

, 22

Prerequisites 22

Surveyingthe InstallationLayout , 23

UsingtheMongoDBShell... 23

Installing

Additional Drivers 24

Installing the PHP driver 25 ConfirmingYour PHP Installation Works 28 Installing the PythonDriver .30

ConfirmingYourPyMongo Installation Works , , 33

(4)

HCONTENTS

Summary

33

Chapter

3: The Data Model 35

Designing

the Database 35

DrillingDownonCollections 36

UsingDocuments 38

CreatingthejdField 40

Building

Indexes 41

ImpactingPerformance with Indexes 42

Implementing Geospatial Indexing

42 Querying Geospatial Information 43

Using

MongoDB

in the Real World 46

Summary

46

Chapter 4:

Working

with Data 47

Navigating

Your Databases 47 ViewingAvailable Databases and Collections 47

Inserting

DataintoCollections 48

Querying

for Data 49

Usingthe Dot Notation 51

UsingtheSort,Limit,andSkip Functions 52

Workingwith Capped Collections, NaturalOrder,and$natural 53

RetrievingaSingleDocument 55

Using the

Aggregation

Commands 55

Workingwith Conditional Operators 57

Leveraging Regular Expressions 65

Updating

Data 65

Updatingwith updateO 65

ImplementinganUpsertwiththesaveQ Command 66 UpdatingInformationAutomatically 66

Specifyingthe PositionofaMatchedArray 70

(5)

Atomic Operations 71 ModifyingandReturningaDocumentAtomically 73

Renaming

a Collection 74

Removing

Data 74

Referencing

a Database 75

Referencing DataManually 75

ReferencingData with DBRef 76

Implementing

Index-Related Functions 78

Surveying Index-RelatedCommands 80

Forcing aSpecifiedIndex to QueryData 80 Constraining QueryMatches 80

Summary

81

Chapter

5: GridFS 83

Filling

in Some

Background

83

Working

with GridFS 84

Getting

Started with the Command-LineTools 85

UsingtheJdKey 86

Working with Filenames 86 DeterminingaFile'sLength 86

Workingwith Chunk Sizes 87 Trackingthe UploadDate 87

HashingYourFiles 87

Looking

Under

MongoDB's

Hood 88

Using the Search Command 90

Deleting , , 90

Retrieving FilesfromMongoDB

, 91

Summingupmongofiles , 91

Exploiting

the Power of

Python

91

Connectingtothe Database 92

(6)

mCONTENTS

AccessingtheWords 93

Putting

Files into

MongoDB

93

Retrieving

Files fromGridFS 94

Deleting

Files 94

Summary

95

Part II:

Developing

97

Chapter

6: PHP and

MongoDB

99

Comparing

Documents in

MongoDB

and PHP 99

MongoDB

Classes 100

Connecting

and

Disconnecting

101

Inserting

Data 102

Listing

YourData 104

ReturningaSingleDocument 104

ListingAll Documents 105

Using Query Operators 106

Querying forSpecificInformation 106 Sorting, Limiting,andSkippingitems 107

Countingthe Number ofMatching Results 108

GroupingData withMap/Reduce 109

Specifyingthe Index withHint 111

RefiningQueries with Conditional Operators 111

Regular Expressions 118

Modifying

Data with PHP 119

UpdatingviaupdateQ 119

SavingTime with ModifierOperators 121

UpsertingData withsave() 125

ModifyingaDocumentAtomically 126

Deleting

Data 129

(7)

Retrievingthe Information 132

GridFS and the PHP Driver 132 StoringFiles 133

AddingMore Metadata to Stored Files 133

Retrieving Files 134

DeletingData 135

Summary

• 135

Chapter

7:

Python

and

MongoDB

137

Working

with Documentsin

Python

137

Using PyMongo

Modules 138

Connecting

and

Disconnecting

138

Inserting

Data 139

Finding

Your Data 140

FindingaSingleDocument 140

FindingMultipleDocuments 141

Using Dot Notation 142

Returning Fields 142 SimplifyingQuerieswithSort, Limit,andSkip 143

Aggregating Queries 145

Specifying

anIndex withHintO • 147

Refining Queries with Conditional Operators 148 ConductingSearches with Regular Expression 153

Modifying

the Data 154

UpdatingYourData 154

ModifierOperators 156

Saving Documents Quickly withSave() 160

Modifyinga DocumentAtomically 161 Puttingthe Parameters to Work 161

Deleting

Data , 162

(8)

m CONTENTS

Creating

a Link Between Two Documents 163

Retrievingthe Information 165

Summary

166

Chapter

8:

Creating

a

Blog Application

with the PHP Driver... 167

Designing

the

Application

168

Listing

the Posts 169

Paging with PHP andMongoDB 171

Looking

ata

Single

Post 172

SpecifyingAdditional Variables 173

Viewingand Adding Comments 174

Searching

the Posts 175

Adding, Deleting,

and

Modifying

Posts 176

Adding aNew Post 177

Editinga Post 178

DeletingaPost 179

Creating

the Index

Pages

180

Recapping

the

blog Application

181

Summary

190

Part III: Advanced 191

Chapter

9: Database Administration 193

Using

AdministrativeTools 194

mongo, the MongoDB Console 194

UsingThird-PartyAdministrationTools 194

Backing

up the

MongoDB

Server 194

Creating aBackup101 194

Backing up aSingleDatabase 197

Backing up aSingleCollection 197

Digging

Deeper

into

Backups

197

Restoring

Individual Databases or Collections 198

(9)

RestoringaSingleDatabase • 199

Restoring aSingleCollection 199

Automating Backups

199

UsingaLocal Datastore 199

Using aRemote(Cloud-Based) Datastore 202

Backing

up

Large

Databases 203

UsingaSlave ServerforBackups • 203

Creating Snapshots withaJournaling Filesystem 203

DiskLayoutto Use with VolumeManagers 205

Importing

Data into

MongoDB

206

Exporting

Data from

MongoDB

207

Securing

Your Data 208

RestrictingAccess toa MongoDB Server 208

Protecting

Your Server withAuthentication 208

AddinganAdmin User 209

Enabling Authentication 209 Authenticatingin the mongo Console 209 ChangingaUser's Credentials 210 Adding aRead-Only User 211

DeletingaUser 211

UsingAuthenticated Connections in aPHPApplication 212

Managing

Servers 212

StartingaServer 212

Reconfiguring

aServer 213

Gettingthe Server's Version 214

Gettingthe Server's Status 214

ShuttingDowna Server 216

Using

MongoDB Logfiles

217

Validating

and

Repairing

Your Data , 217

RepairingaServer , ,„„. 217

(10)

aCONTENTS

ValidatingaSingleCollection 218

RepairingCollection ValidationFaults 219 RepairingaCollection's Datafiles 220

Upgrading MongoDB

221

Monitoring MongoDB

221

RollingYourOwn Stat Monitoring Tool 222

Using

the

mongod

Web Interface 223

Summary

223

Chapter

10:

Optimization

225

Optimizing

Your Server Hardware for Performance 225

UnderstandingHowMongoDB UsesMemory 225

ChoosingtheRightDatabase Server Hardware 226

Evaluating Query

Performance 226

MongoDB

Profiler 226

Enabling andDisablingthe DBProfiler 227 AnalyzingaSpecific Querywith explainO 228

UsingProfile andexplainO toOptimizeaQuery 229

Managing

Indexes 232

ListingIndexes 233

CreatingaSimple Index 233

Creating aCompound Index 234

Specifying

Index

Options

235 Creatingan Index in the Backgroundwith{background:true} 235

CreatinganIndexwith aUnique Key {unique:true} 236

Dropping Duplicates Automaticallywith{dropdups:true} 236

Dropping an Index 236

Re-IndexingaCollection 237

How

MongoDB

SelectsWhich Indexes It Will Use 237

Using

HintQ

to Force

Using

a

Specific

Index 238

(11)

Optimizing

the

Storage

of Small

Objects

238

Summary

• 239

Chapter

11:

Replication

• »241

Spelling

Out

MongoDB's

Replication

Goals 242

ImprovingScalability 242 Improving Durability/Reliability 242

ProvidingIsolation 243

Drilling

Down onthe

Oplog

...243

Implementing Single

Master/Single

Slave

Replication

244

Setting Up aMaster/SlaveReplication Configuration 245

Implementing Single Master/Multiple

Slave

Replication

248

Configuring

a Master/Slave

Replication System

248

Resynchronizing

aMaster/Slave

Replication System

....249

Issuing aManualResyncCommand tothe Slave 250

Resyncing byDeletingtheSlaves Datafiles 250 ResyncingaSlavewiththe --fastsync Option 250

Implementing

Multiple Master/Single

Slave

Replication

.251

Setting upaMultipleMaster/Slave Replication Configuration 251

Exploring

Various

Replication

Scenarios ....254

ImplementingCascadeReplication 254 ImplementingMaster/MasterReplication 254 ImplementingInterleaved Replication 255

Using

Replica

Pairs 256

ResolvingServerDisputeswith anArbiter 261

Implementing

Advanced

Clustering

with

Replica

Sets 262

Creating aReplicaSet 264

GettingaReplicaSet MemberUpandRunning 265

Adding aServertoaReplica Set ...266

Managing ReplicaSets , ., ,..,267

(12)

CONTENTS

ConfiguringtheOptionsforReplicaSet Members 271 DeterminingtheStatusofReplicaSets 273 ConnectingtoaReplicaSetfrom YourApplication 273

Summary

275

Chapter

12:

Sharding

277

Exploring

the Need for

Sharding

277

Partitioning

Horizontal andVertical Data 278

PartitioningDataVertically 278

PartitioningData Horizontally 278

Analyzing

a

Simple

Sharding

Scenario 279

Implementing

Sharding

with

MongoDB

280

Setting Up

a

Sharding

Configuration

282

AddingaNew Shard to the Cluster 285

Removing

aShard fromthe Cluster 287

Determining

HowYou're Connected 288

Listing

the Status ofaSharded Cluster 288

Using

Replica

SetstoImplement Shards 290

Sharding

to

Improve

Performance 290

Summary

291

tIndex 293

References

Related documents

The following empirical equation is used to calculate for the overall heat transfer coefficient from Perry’s Handbook with the index n = 0.67 (McCormick, 1962)... Assuming

In April 1974 the Parish Halls passed to the East Grinstead Town Council as the successor authority to the East Grinstead Urban District Council.. Over the next couple of years

ROTARY CLUB OF BEAUMARIS BULL ET IN – SERVING THE COMMUNIT Y SINCE 1985 This Week’s Meeting. PP David Hone finally got to hand out some of the Paul Harris Fellowship Awards from his

Respon siswa terhadap pembelajaran dengan bantuan modul praktikum berbasis model PBI dengan scientific approach yang diterapkan pada mata pelajaran instalasi

for leading business schools, higher education establishments and universities With the participation of nearly 50 wine clubs in the world, The Left Bank Bordeaux Cup becomes

Enjoy lunch on board before walking along the Burgundy Canal to visit some of the small villages.. Opt to take a bicycle out for ride ahead

WALK FOR LIFE ANGELS CAMP SATURDAY, JANUARY 30 TH , 2021. Due to the lack of registrations, we have cancelled the bus to take us to Walk for Life West Coast

The Master unit will accept input for single remote controller (wired or wireless) and will replicate the inputs to all Slave units.. Slave units can not accept single