MongoDB Introduction - Document Oriented Nosql Database

1,522 views

Published on

Published in: Software
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,522
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
41
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

MongoDB Introduction - Document Oriented Nosql Database

  1. 1. By : Sudhir Patil @spatilTweet
  2. 2. I will talk …. •What is MongoDB ? •Queries in MongoDB •Replication •Sharding •Other Cool Features •Code samples •Performance • When to Use & not Use?
  3. 3. What is MongoDB? •Document-oriented •JSON-style documents (Actually BSON) •Dynamic Schema •Non Relational (NOSQL) •Performance •Written in C++ for high •Full Index support •No Transactions (but supports atomic operations) •Not relational •Scalability •Replication •Shardinges
  4. 4. Who is using MongoDB? See more @ http://www.mongodb.org/display/DOCS/Production+Deployments
  5. 5. Document Oriented?? You can store and access data in real life document style. - Can have any number of fields. - Can contain other documents(comments in below example) - Can store array/collection elements (e.g tags) - Not restricted to strict columns and rows. Document can con For RDBMS people, its denormalized view by combining multiple tables relational databases. { “title”: “NoSQL Database Internals”, “date”: “Tue Oct 27 2009 17:24:18 GMT-0400 (EDT)”, “content” : “ Database internals …….”, “author” : “xxxxxx” , “tags”: [“nosql”, “databases”, “c++”], “comments”: [ {“author”: “Fred”, “text”: “Quite” } ] }
  6. 6. How to access/query data? db.post.find() ➔ SELECT * FROM post db.posts.find({“author”:“Kyle”}) db.posts.find(“tags”: “nosql”) db.posts.find({“date”: {“$lte”: Date.now}) db.post.find({“comments.username”: “eh”})
  7. 7. Insert/Update/Alter? Insert db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25}) ➔ INSERT INTO user (`name`, `age`) values ('foobar',25) Update db.user.update({'name' : 'foobar'}, {$set : {'age' : 36}}) ➔ UPDATE user SET `age` = 36 WHERE `name` = 'foobar‘ if you want add a column `email` db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25, ‘email’ : ‘foo@bar.com’}) ➔ ALTER TABLE user…. Delete db.user.remove({'age' : {$lt : 30}}) ➔ DELETE FROM user WHERE age < 30
  8. 8. But why should i use instead of RDBS(say mysql)? ● If you got huge data, thats difficult to manage in RDBMS, without lot of firefight. ● Dynamic schema, application can add new fields just by adding fields in Object mapping collection(table). ● Faster development, as you don’t need alter table scripts and don’t need to split your data into columns. ● Easier to scale and shard. ● New shiny thing?
  9. 9. DriversDrivers / Libraries •All most all Famous languages have drivers for mongodb. More details @ http://www.mongodb.org/display/DOCS/Drivers •OR M (Object relational mappers) o Java - morphia, spring data o Ruby - mongoid, mongoMapper o Php - doctrine, mongodloid o node.js - Mongoose.
  10. 10. Features: Replication •Master-Slave Replication •Replica Pairs
  11. 11. Features : Sharding
  12. 12. Other Cool Features • Grid FS • Store files of any size • GridFS will leverage any existing replication or autosharding that you’ve set up for MongoDB, so getting failover and scale-out for file •Map Reduce •Queries run in all shards parallel •Flexible aggregation and data processing •Geospatial Indexing •Find objects based on location – Find closest n items to x e.g db.map.insert({location : {longitude : -40, latitude : 78}}) db.map.find({location : {$near : [ -30, 70]})
  13. 13. Performance Source: http://www.idiotsabound.com/did-i-mention-mongodb-is-fast-way-to-go-mongo |+-------------------------------------------------------------------------+| **** 100000 Indexed Queries **** |+-------------------------------------------------------------------------+| | Average | Median | Deviation | Lap Total | Total | +-------------------------------------------------------------------------+ | MongoDb | 0.00025 | 0.00021 | 0.00019 | 24.99955 | 29.138463 | | CouchDb | 0.01098 | 0.01067 | 0.00972 | 1097.85109 | 1101.09003 | | MysqlDb | 0.00199 | 0.00032 | 0.00518 | 199.32546 | 201.88513 | +---------------------------------------------------------------------------+
  14. 14. Source: http://www.michaelckennedy.net/blog/2010/04/29/MongoDBVsSQLServer2008PerformanceShowdown.aspx Performance
  15. 15. Code Samples : Music Band Band Contact Info Distributor Song 1 10…. * 0…. *
  16. 16. When Not to use it / Drawbacks? • If your data is not huge and comfortably fits in RDBMS • If you need transactional features. • If you have complex Relationships. • Learning curve, querying etc is not hard in mongo but almost everyone knows SQL syntax. • Global write lock. • Duplicate data and data integrity as same piece of data may be embedded in multiple documents. • You have to manage any relationships at application layer.
  17. 17. Nuggets • Consider it only when your data is huge or doesn’t really suit relational structure. • Very easy & enjoyable at development phase but as application grows data stored might get messy. So store and retrieve data using ORM objects only and be careful while changing object fields e.g renaming. • Relationships ,constraints, integrity etc have to be managed at application level, unlike RDBMS where all these are built in. • Don’t stuff everything in single document, use references to other documents when appropriate. • Querying is not hard but takes some time to get used to it, so not easy as querying in SQL.
  18. 18. Thank You!

×