25
MongoDB “Walking on water and developing software from a specification are easy if both are frozen.” BY : NklMish @nklmish

Mongo - an intermediate introduction

  • Upload
    nklmish

  • View
    483

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Mongo - an intermediate introduction

MongoDB“Walking on water and developing software from a

specification are easy if both are frozen.”

BY :NklMish

@nklmish

Page 2: Mongo - an intermediate introduction

Why & What?FeaturesAggregation framework overviewSharding and Replica SetSummary

Agenda

Page 3: Mongo - an intermediate introduction

This talk is not about :NoSqlRelational vs Non-RelationalComparison of other NoSql flavors

NOTE

Page 4: Mongo - an intermediate introduction

“One size fits all” approach no longer appliesNoSql DatabaseSchemaLess != NoSchemaDocument based ApproachNon-Relational Dbs scale more easily, especially horizontallyIn a nutshell focus on speed, performance, flexibility and scalability.

What is MongoDB?“Nothing endures but change”

Page 5: Mongo - an intermediate introduction

Agility.High Scalability => horizontal => thousand of nodes or clouds or across multiple data centersRich Indexing

Real life example:

Server Density

OTTO

Expedia, Forbes, MetLife,Bosch, etc.

Why?“Luck is not a factor. Hope is not a strategy. Fear is not an option”

Page 6: Mongo - an intermediate introduction

RDBMSTablesRecords/rowsQueries return records

Mapping“You improvise. You adapt. You overcome.”

MongoDBCollectionsDocuments/objectsQueries return a cursor ???? Because of performance, efficiency

Page 7: Mongo - an intermediate introduction

Standard Db FeaturesDocs are stored in BSON => Mongo understands JSON natively => Any Valid JSON can be imported and queried(E.g. mongoimport -f foo.json).Map Reduce Aggregation FrameworkGridFS (for Efficient binary large objects )GeoNear

Features“Stable Velocity. Sustainable Pace.”

Page 8: Mongo - an intermediate introduction

$match – filter docs$project – reshape docs$group – Summarize docs$unwind – Expand docs$sort – Order docs$limit/$skip – paginate docs$redact – Restrict docs$geoNear – Proximity sort docs$let, $map – Define variables

Aggregation In Nutshell“Talk is cheap. Show me the code”

Page 9: Mongo - an intermediate introduction

{name : “Java”,price : 250,Type : “ebook”}{name : “Php”,price : 200,Type : “ebook”}{name : “Javascript”,price : 150,Type : “hardCopy”}

Matching{$match : {

Type : “ebook”}}

{name : “Java”,price : 200,Type : “ebook”}{name : “Php”,price : 200,Type : “ebook”}

Page 10: Mongo - an intermediate introduction

{name : “Java”,price : 250,Type : “ebook”}{name : “Php”,price : 200,Type : “ebook”}{name : “Javascript”,price : 150,Type : “hardCopy”}

Query Operator{$match : {

price : {$gt : 200}}}

{name : “Java”,price : 250,Type : “ebook”}{name : “Php”,price : 200,Type : “ebook”}

Page 11: Mongo - an intermediate introduction

{name : “Java”,price : 250,Type : “ebook”}{name : “Php”,price : 200,Type : “ebook”}

Including Excluding part of document{$project : {

name :1,price : 1,Type : 0

}}

{name : “Java”,price : 200”}{name : “Php”,price : 200}

Page 12: Mongo - an intermediate introduction

{name : “Java”,price : 250,Type : “ebook”,quantity : 3}{name : “Php”,price : 200,Type : “ebook”,quantity : 2}

Custom Field Computation{$project : {fullStock : {

$mul : [“$price”, “$quantity”]},Title : “$name”}}

{fullStock : 750,Title: “Java”}{price : 400,Title: “Php”}

Page 13: Mongo - an intermediate introduction

{name : “Java”,price : 250,Type : “ebook”,quantity : 3}{name : “Php”,price : 200,Type : “ebook”,quantity : 2}

Generating Sub-Document {$project : {fullStock : {

$mul : [“$price”, “$quantity”]},details : {Title : “$name”, quantity : “$quantity” }}}

{fullStock : 750,details : {Title: “Java”, quantity : 3}},{,fullStock : 400,details : {Title: “Php”, quantity : 2}}

Page 14: Mongo - an intermediate introduction

$group

Group Docs by valueBy default in memory processingHelpful operators that go with:$max, $min, $avg, $sum$addToSet, $push$first, $last

Page 15: Mongo - an intermediate introduction

{name : “Java Fun”,publisher : “manning”,price : 1000}{name : “Oracle Fun”,publisher : “manning”,price : 1000}{name : “Php Fun”,publisher : “Orelly”,price : 400}

Compute Average{$group : {_id : “$publisher”,avgPrice : {$avg : “$price” }}}

{_id: manning,avgPrice :1000},{_id : Php,,avgPrice: 400}

Page 16: Mongo - an intermediate introduction

$unwind

Useful for doc containing array fields:Create docs from array entriesEntries can be replace by value

Page 17: Mongo - an intermediate introduction

{publisher :“manning”,

title: [“java”, “Php”],discount : 50%}

$unwind{$unwind : $category}

{publisher : manning,title: java,discount :50%},{publisher : manning,title: Php,discount :50%}

Page 18: Mongo - an intermediate introduction

$redact

Restrict access to docs based on doc fields to define privilegesUseful terminology $$DESCEND, $$PRUNE, $$KEEP

Page 19: Mongo - an intermediate introduction

{_id :123,name : logo,security : “ANYONE”,Profit : {

security : “MARKETING”,

revenue : 500%,ProfitByCountry : {

security : “BOARD_OF_DIRECTOR”PL : 800 %NO : 700 %

DK : 600%SW : 500 %

}}

}

$redactdb.products.aggregate([

{$match : {name : “logo”}}, $redact : {

$cond : {if : {$eq : …},then : “$$DESCEND”,

else : “$PRUNE }}])

{...}

Page 20: Mongo - an intermediate introduction

Sharding(Horizontal scaling)

Page 21: Mongo - an intermediate introduction

ShardingAllow to store data across multiple machines.Ok but what for ?Database systems with large data sets and high throughput applications can challenge the capacity of a single server.High query rates can exhaust the CPU capacity of the server.Larger data sets exceed the storage capacity of a single machineWorking set sizes larger than the system’s RAM stress the I/O capacity of disk drives.

Page 22: Mongo - an intermediate introduction

Replica Set

Page 23: Mongo - an intermediate introduction

Replica SetGroup of mongod processes that maintain the same data set. Provides redundancy and high availability, in a nutshell basis for all production deployments.Min 3 nodes required E.g.

Page 24: Mongo - an intermediate introduction

Document oriented db.Scale and performs wellProvide powerful aggregration frameworkTested on massive datasets.Support for map reduce.

Summary

Page 25: Mongo - an intermediate introduction

Questions ????