An introduction to Mongo DB Rcz Gbor ELTE
An introduction to Mongo. DB Rácz Gábor ELTE IK, 2013. febr. 10.
In Production 2 http: //www. mongodb. org/about/production-deployments/
No. SQL • Key-value • Graph database • Document-oriented • Column family 3
Document store RDBMS Mongo. DB Database Table, View Collection Row Document (JSON, BSON) Column Field Index Join Embedded Document Foreign Key Reference Partition Shard 4
Document store RDBMS Mongo. DB Database Table, View Row Column Index Join Foreign Key Partition > db. user. find. One({age: 39}) { Collection "_id" : Object. Id("5114 e 0 bd 42…"), Document (JSON, BSON) "first" : "John", Field "last" : "Doe", Index "age" : 39, "interests" : [ Embedded Document "Reading", Reference "Mountain Biking ] Shard "favorites": { "color": "Blue", "sport": "Soccer"} } 5
CRUD • Create • db. collection. insert( <document> ) • db. collection. save( <document> ) • db. collection. update( <query>, <update>, { upsert: true } ) • Read • db. collection. find( <query>, <projection> ) • db. collection. find. One( <query>, <projection> ) • Update • db. collection. update( <query>, <update>, <options> ) • Delete • db. collection. remove( <query>, <just. One> ) 6
CRUD example > db. user. insert({ first: "John", }) last : "Doe", age: 39 > db. user. update( {"_id" : Object. Id("51…")}, { $set: { age: 40, salary: 7000} } ) > db. user. find () { "_id" : Object. Id("51…"), "first" : "John", "last" : "Doe", "age" : 39 } > db. user. remove({ "first": /^J/ }) 7
Features • Document-Oriented storege • Full Index Support • Replication & High Availability • Auto-Sharding • Querying • Fast In-Place Updates • Map/Reduce Agile Scalable 8
Memory Mapped Files • „A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. ” 1 • mmap() 9 1: http: //en. wikipedia. org/wiki/Memory-mapped_file
Replica Sets • Redundancy and Failover • Zero downtime for upgrades and maintaince • Master-slave replication • Strong Consistency • Delayed Consistency • Geospatial features Host 1: 10000 Host 2: 10001 Host 3: 10002 replica 1 Client 10
Sharding • Partition your data • Scale write throughput • Increase capacity • Auto-balancing shard 1 Host 1: 10000 shard 2 Host 2: 10010 configdb Host 3: 20000 11 Host 4: 30000 Client
Mixed shard 1 Host 1: 10000 Host 2: 10001 . . . shardn Host 4: 10010 Host 3: 10002 replica 1 configdb Host 5: 20000 Host 6: 30000 Host 7: 30000 Client 12
Map/Reduce db. collection. map. Reduce( <mapfunction>, <reducefunction>, { out: <collection>, query: <>, sort: <>, limit: <number>, finalize: <function>, scope: <>, js. Mode: <boolean>, verbose: <boolean> } ) var map. Function 1 = function() { emit(this. cust_id, this. price); }; var reduce. Function 1 = function(key. Cust. Id, values. Prices) { return sum(values. Prices); }; 13
Other features • Easy to install and use • Detailed documentation • Various APIs • Java. Script, Python, Ruby, Perl, Java, Scala, C#, C++, Haskell, Erlang • Community • Open source 14
Theory of no. SQL: CAP C • Many nodes • Nodes contain replicas of partitions of data • Consistency • all replicas contain the same version of data • Availability • system remains operational on failing nodes • Partition tolarence • multiple entry points • system remains operational on system split A P CAP Theorem: satisfying all three at the same time is impossible 15
Theory of no. SQL: CAP C • Many nodes • Nodes contain replicas of partitions of data • Consistency • all replicas contain the same version of data • Availability • system remains operational on failing nodes • Partition tolarence • multiple entry points • system remains operational on system split A P CAP Theorem: satisfying all three at the same time is impossible 16
ACID - BASE • • Atomicity Consistency Isolation Durability • Basically Available (CP) • Soft-state • Eventually consistent (AP) 17 Pritchett, D. : BASE: An Acid Alternative (queue. acm. org/detail. cfm? id=1394128)
Thank you for your attention! 18
- Slides: 18