MongoDB Er. Shiva K. Shrestha ME Computer, NCIT 6/20/2016 Er. Shiva K. Shrestha ME Computer, NCIT Distributed Operating System
Introduction MongoDB = “Humongous DB” Open-source & Cross-platform 6/20/2016 MongoDB = “Humongous DB” Open-source & Cross-platform Document-based “High Performance, High Availability” Automatic Scaling 10gen => MongoDB Inc. 2009 Stable Release: 8th March, 2016 High Performance: JSON Format, Less CPU Overhead, Data assessable from any Node Scaling: -> Cluster Scale -> Performance Scale -> Data Scale
Database Evolution Flat Files RDBMS NoSQL MongoDB 1970s: data stored in flat file system 1979: a standard way of query the data - what do we want to query, how to process that query but 1998: 1. Key-value Store 2. Tabular 3. Document oriented (MongoDB, CouchDB, etc.) 2009: Free & Open Source, Cross-Platform, Document Oriented Problem: no standard implementation Problem: with the big n huge amount data Problem: Less support for Complex Queries Solution: Relational Database Solution: NoSQL Solution: Use Third-Party GUIs 6/20/2016
Features Expressive Query Language & Secondary Indexes Strong Consistency Flexibility Scalability & Performance 6/20/2016 Easy Assess Provide up-to-date copy of data Easily added extra column without affecting others Secured Monitored, automated & integrated with our existing infrastructure Millions ops/sec, 100 Billions Documents, Peta Bytes of Data
MongoDB is: General Purpose Easy to Use Fast & Scalable Rich data model Full featured indexes Sophisticated query language Easy to Use Easy mapping to object oriented code Native language drivers in all popular languages Simple to setup and manage Rich Data Model: Any type of data can be stored Fast & Scalable Operates at in-memory speed wherever possible Auto-sharding built in Dynamically add / remove capacity with no downtime
Comparisons RDBMS MongoDB Relational database Document-oriented database, non relational Schema based database, follow joins Schema-less database, references and $lookup Data stored in tables (tables contain collection of rows) Data stored in form of collection (collection contain group of object document) No horizontal scaling Eg. 1st row has 5 column then 2nd row must have 5 column for the given table Horizontal scaling Eg. 1st row has 5 column then 2nd row can have any no of column for the given table SQL queries are used JSON, BSON format 6/20/2016
Data Format JSON XML {"employees“ :[ {"firstName":“Anju", "lastName":“Shah"}, {"firstName":“Sumit Kumar", "lastName":“Shah"}, {"firstName":“Shiva K.", "lastName":“Shrestha"} ]} <employees> <employee> <firstName>Anju</firstName> <lastName>Shah</lastName> </employee> <employee> <firstName>Sumit Kumar</firstName> <lastName>Shah</lastName> </employee> <employee> <firstName>Shiva K.</firstName> <lastName>Shrestha</lastName> </employee> </employees> 6/20/2016
Advantages Schema less Structure of single object is clear No complex joins Document-based Query language Conversion/Mapping is not needed Use internal memory, hence faster 6/20/2016
Why and Where to use Object oriented storage style (JSON) Index on any attributes Replication and Availability Auto-sharding Big data Content management system Mobile and social infrastructure Data Hub 6/20/2016
Terminology Translation SQL Terms/Concepts MongoDB Terms/Concepts Database Table Collection Row Document or BSON document Column Field Index Table joins Embedded documents & linking Unique column or column combination as primary key. Primary key is automatically set to the _id field. Aggregation (e.g. group by) Aggregation pipeline 6/20/2016
CRUD Operations Insertion: Update: Delete: SQL MongoDB Command INSERT INTO t (fn, ln) VALUES (‘Saroj', ‘Shakya') db.t.insert({fn:‘Saroj', ln: ‘Shakya'}) 6/20/2016 SQL MongoDB Command UPDATE t SET ln='Nepal' WHERE ln='Shakya' db.t.update({ln:‘Shakya'},{$set:{ln:‘Nepal'}}) SQL MongoDB Command DELETE FROM t WHERE fn=‘Saroj' db.t.remove({fn:‘Saroj'})
Retrieving Data SQL Statement MongoDB Commands SELECT * FROM table db.collection.find() WHERE artist = ‘Rekha’ db.collection.find({Artist:”Rekha”}) SELECT* ORDER BY Title db.collection.find().sort(Title:1) DISTINCT .distinct() GROUP BY .group() >=, < $gte, $lt 6/20/2016
Replication 6/20/2016
Knee Curve 6/20/2016
Drivers & Frameworks 6/20/2016 MEAN Stack Morphia
Conclusions MongoDB is fast Very little CPU overhead Implemented in C++ for best performance Free & Open Source, Cross-Platform, Document Oriented Easier and faster integration of data Very rapid development 6/20/2016
Q/A Session: 6/20/2016 Thank You!