mongo db 101 dc group

41
MongoDB 101

Upload: john-ragan

Post on 14-Jun-2015

912 views

Category:

Technology


2 download

DESCRIPTION

An introduction to MongoDB. You can exercise the examples against 10gen's zips.json file.

TRANSCRIPT

Page 1: Mongo db 101 dc group

MongoDB 101

Page 2: Mongo db 101 dc group

Agenda

• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing

Page 3: Mongo db 101 dc group

Agenda

• Aggregation• Joins and Transactions• Replica Sets• Sharding

Page 4: Mongo db 101 dc group

Document JSON Example

{"_id" :

ObjectId("72f494c1c3df14726f1403b3"),"city" : "Vienna", "zipcode" : "22180","pop" : 20795 "surveyDate" : new Date()

}

Page 5: Mongo db 101 dc group

Embedded Example{

"name" : {"first" : "Joe","last" : "Schmoe"

},"address" : {

"street" : "123 Maple Avenue""city" : "Ashburn""state" : "VA""zipcode" : "20148"

}"age" : 45

}

Page 6: Mongo db 101 dc group

Another Example{ "title" : "MongoDB 101", "author" : "John Ragan", "content" : "My thoughts on MongoDB", "comments" : [

{ "name" : "Jake the Troll", "comment" : "My trollish comments",

{ "name" : "Dwight Merriman", "lastName" : ”Insightful comments from Dwight", },

{ "name" : "Jake the Troll", "comment" : "My even more trollish comments“,

], "tags" : [ "mongodb", "101" ]}

Page 7: Mongo db 101 dc group

Documents

• Analogous to a database row• Schema-free• Keys and Values– Strings– Value types

• Special key: _id– unique

Page 8: Mongo db 101 dc group

MongoDB and Relational Corollaries

Relational Database MongoDB

Database instance Mongo instance

Database(s) Database(s)

Table(s) Collection(s)

Row(s) Document(s)

Values Keys and Values

Page 9: Mongo db 101 dc group

Easy to Get Started

Page 10: Mongo db 101 dc group

Insert

zip = {"city" : "Ashburn","loc" : [ -77.480612, 39.039918],"pop" : 19416, "state" : "VA”,"_id" : "20148"

}db.census.insert(zip)

Page 11: Mongo db 101 dc group

Insert

• JSON converted to BSON• Must be less than 16 Mgs (in BSON)• Adds _id unless already specified

Page 12: Mongo db 101 dc group

Find

find() findOne()

db.census.findOne({"_id": "22180"}

)

Page 13: Mongo db 101 dc group

Projection

> db.census.find({}, {"city" : 1, "state" : 1}){

"_id" : 22180,"city" : "VIENNA", "state" : "VA"

}

db.census.find({}, {"loc" : 0})

Page 14: Mongo db 101 dc group

Query Conditionals

• $gt• $gte• $lt• $lte• $ne• $not

db.census.find({"_id" : {"$gte" : "70300", "$lte" : "70399"}})

Page 15: Mongo db 101 dc group

In and Not In

• $in• $nin

db.census.find({"_id" : {"$in" : ["22180", "70301", "22030"]}})

Page 16: Mongo db 101 dc group

OR Queries

db.census.find({"$or" : [

{"_id" : {"$in" : ["22180", "90210"]}},{"city" : "ASHBURN"}

]})

Page 17: Mongo db 101 dc group

Regular Expressions

• Perl Compatible Regular Expression (PCRE)

db.census.find({"city" : /^ASHBU?/i}

)

Page 18: Mongo db 101 dc group

Limits, Skips and Sorts

db.census.find({"city" : "CHICAGO"}

).skip(3).limit(4).sort("zipcode" : -1})

Page 19: Mongo db 101 dc group

Update

update{ <criteria>, <new doc> }

db.census.update({_id : "22011"}, {city : "BROADLANDS"}

)

Page 20: Mongo db 101 dc group

Update - $set and $unset

db.users.update({"name" : "joe"},{"$set" : {"favorite book" : "harry potter"}})

db.users.update({"name" : "joe"}, {"$unset" : {"favorite book" : 1}})

Page 21: Mongo db 101 dc group

Delete

db.census.remove( <criteria> )

db.census.remove({city : "NORTH POLE"})

db.census.remove()

db.drop_collection("census")

Page 22: Mongo db 101 dc group

Indexes

db.census.ensureIndex({"city" : 1})

FAST:db.census.find().sort("city" : 1})

SLOW:db.census.find().sort({"pop" : 1, "city" : 1})

db.census.ensureIndex({"pop" : 1, "city" : 1})

Page 23: Mongo db 101 dc group

Index Ordering

db.census.ensureIndex({"pop" : 1, "city" : 1})

Fast or Slow?

db.census.find().sort({"pop" : -1, "city" : 1})

Page 24: Mongo db 101 dc group

Other Index Options

db.census.ensureIndex({"city" : 1}, {

"name" : "myIndex","unique" : true,"dropDups", true

})

Page 25: Mongo db 101 dc group

Explain

• explain will return information– indexes used for the query (if any)– stats about timing– the number of documents scanned

db.census.find({city:"CHICAGO"}).explain()

Page 26: Mongo db 101 dc group

Aggregation Framework

• Largest and smallest cities in Virginia, California and Louisiana

Page 27: Mongo db 101 dc group

MongoDB

• Relational Databases are Dead

Page 28: Mongo db 101 dc group

MongoDB

• Relational Databases are Dead– Of course that is not true!– Right Tool for the Right Job

Page 29: Mongo db 101 dc group

Why MongoDB?

• Schema flexibility• Developer speed• Horizontal scalability

Page 30: Mongo db 101 dc group

Developer Flexibility

“An elephant should not always have to sit on your data before you persist it”

Page 31: Mongo db 101 dc group

Increasing Horizontal Scalability

• No joins– Thus, no distributed joins

• No transactions– Thus, no distributed transactions

Page 32: Mongo db 101 dc group

Life Without Joins

• Already denormalized or Reference Id’s• One to One relationships• One to Many relationships• Many to Many references

Page 33: Mongo db 101 dc group

Life Without Transactions

• Document Level transaction boundaries• Nesting within documents• Two Phase commit

Page 34: Mongo db 101 dc group

Update - $inc

{"url" : "www.example.com","pageviews" : 52}

db.analytics.update({"url" : "www.example.com"}, {"$inc" : {"pageviews" : 1}})

{"url" : "www.example.com","pageviews" : 53}

Page 35: Mongo db 101 dc group

Replica Sets

• Primary-Secondary cluster– Automatic failover– Primary elected by cluster

• One Primary, many Secondary– Others

• Fully automatic– It handles voting, etc.

• 3 Node viable minimum

Page 36: Mongo db 101 dc group

Demo Replica Set Failover

Page 37: Mongo db 101 dc group

Sharding

• The process of splitting data up and storing different portions of the data on different machines

• Automatic vs. manual• Chunks– Shard Key

Page 38: Mongo db 101 dc group

Mongod Mongod Mongod

Mongos

Client

Page 39: Mongo db 101 dc group

Sharding

• Server types:– Shard

• holds a subset of a collection’s data. – Single mongod server– Replica set

– Mongos• router process and aggregates responses• Does not store anything

– config server• Stores cluster configuration: which data is on which shard.

• Start these in reverse

Page 40: Mongo db 101 dc group

Summary

• Document Structures and Corrollaries• Getting Started• CRUD Operations• Indexing

Page 41: Mongo db 101 dc group

Summary

• Aggregation• Joins and Transactions• Replica Sets• Sharding