Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Mongodb

156 views

Published on

MongoDb is a document oriented database and very flexible one as it gives horizontal scalability.
In this presentation basic study about mongodb with installation steps and basic commands are described.

Published in: Engineering
  • Be the first to comment

Mongodb

  1. 1. MongoDB Introduction to Apurva Vyas Neha Bhardwaj Shivam Mittal Mahendra Reddy
  2. 2. Document Database One of the most popular ways of storing data is a document data model, where each record and its associated data is thought of as a “document”. In a document database, such as MongoDB, everything related to a database object is encapsulated together.
  3. 3. Why use MongoDB? • SQL was invented in the 70’s to store data. But the environment for data, as well as programming, has changed since the development of the SQL database: •The emergence of cloud computing has brought deployment and storage costs down dramatically.
  4. 4. • The need to store unstructured data, such as social media posts and multimedia, has grown rapidly. • Agile development methods mean that the database schema needs to change rapidly as demands evolve
  5. 5. Strategic Priorities Enabling New & Enhancing Existing Apps Better Customer Experience Lower TCOFaster Time to Market
  6. 6. • MongoDB stores documents (or) objects. • Now-a-days, everyone works with objects (Python/Ruby/Java/etc.) • And we need Databases to persist our objects. Then why not store objects directly ? • Embedded documents and arrays reduce need for joins. No Joins and No-multi document transactions.
  7. 7. What is MongoDB ? MongoDb is a Scalable , High-Performance Open-source, Schema-Free , Document-orientated database.
  8. 8. Scalable Non-relational DBs scale more easily, especially horizontally.
  9. 9. High Performance Focus on speed, performance, flexibility. Not concerned with transactional stuff and relational semantics DBs should be an on-demand commodity, in a cloudlike fashion
  10. 10. Schema-Free
  11. 11. Document Oriented Database Store all information for an object in a single instance. • No spanning tables for all information • No more normalization #Each stored object can be different. • Not all stord documents need to contain same data. #handles semi structured,unstructured data.
  12. 12. RDBMS to MONGODB DATABSE DATABASE TABLE COLLECTION TUPLE/ROW DOCUMENT COLOUMN FIELD PRIMARY KEY PRIMARY KEY (DEFAULT KEY _ID PROVIDED BY MONGODB ITSELF)
  13. 13. Hitting RDBMS Limits Data Types •Unstructured data •Semi-structured data •Polymorphic data Volume of Data •Petabytes of data •Trillions of records •Millions of queries per second Agile Development •Iterative •Short development cycles •New workloads New Architectures •Horizontal scaling •Commodity servers •Cloud computing
  14. 14. RDBMS The Power of Dynamic Schema MongoDB { _id : ObjectId("4c4ba5e5e8aabf3"), employee_name: "Dunham, Justin", department : "Marketing", title : "Product Manager, Web", report_up: "Neray, Graham", pay_band: “C", benefits : [ { type : "Health", plan : "PPO Plus" }, { type : "Dental", plan : "Standard" } ] }
  15. 15. Features • Built for Speed • Rich Document based queries for Easy readability. • Full Index Support for High Performance. • Replication and Failover for High Availability. • Auto Sharding for Easy Scalability. • Map / Reduce for Aggregation.
  16. 16. • The queries are written in document format for easy readability and understanding. • A database index is a data structure that improves the speed of data retrieval operations
  17. 17. REPLICATION Process of synchronizing data across multiple servers. Provides redundancy and increases data availability with multiple copies of data on different database servers.
  18. 18. Why Replication • Allows you to recover from hardware failure and service interruptions • Read scaling (extra copies to read from). • High (24*7) availability of data. • No downtime for maintenance (like backups, index rebuilds, compaction)
  19. 19. How Replication Works in MongoDB MongoDB achieves replication by the use of replica set. Replica set is a group of two or more nodes: Primary and Secondary All data replicates from primary to secondary Client application always interacts with primary, replicating the data on secondary.
  20. 20. Sharding Sharding is the process of storing data records across multiple machines. It is MongoDB's approach to meeting the demands of data growth. Sharding solves the problem of increased data size with horizontal scaling.
  21. 21. Why Sharding over Replication ? 1. Single replica set has limitation of 12 nodes. 2. Memory can't be large enough when active dataset is big 3. Latency sensitive queries still go to master 4. Vertical scaling is too expensive
  22. 22. MAP-REDUCE 1. MongoDB applies the map phase to each input document. 2. For multiple key values, MongoDB applies the reduce phase, which collects and condenses the aggregated data. 3. It is same as Group By clause in sql but much more efficient.
  23. 23. RDBMS: Blogging Platform JOIN 5 tables
  24. 24. MongoDB: Denormalized to 2 BSON Documents Higher Performance: Data Locality
  25. 25. Some Cool features • Geo-spatial Indexes for Geo-spatial queries. $near, $within_distance, Bound queries (circle, box) • GridFS Stores Large Binary Files. • Map/Reduce GROUP BY in SQL, map/reduce in MongoDB.
  26. 26. Some Companies using MongoDB in Production
  27. 27. Mongodb Installation MongoDB is a document oriented database. Which provides High performance, High availability Easy scalability. MongoDB works on concept of collection and document.
  28. 28. Installation Process Install MongoDB on Windows Install MongoDB on Windows from https://www.mongodb.org/downloads (or) https://www.mongodb.com.
  29. 29. • check our system type whethere it is 64-bit or 32-bit
  30. 30. • choose setup Type complete then press Next
  31. 31. • Now Press Install
  32. 32. • Now Press Finish
  33. 33. • Now go to my computer.
  34. 34. • Copy the bin Location.
  35. 35. • Now go to drive C: there you add one folder name it as data. Inside data again add another folder db.
  36. 36. Now click on ok.. ok.. ok.. Mongodb successfully installed. open Command Prompt(cmd). Type mongod open another Command Prompt Type mongo
  37. 37. • connecting to Mongodb
  38. 38. • To Run Mongodb directly by using Intelli Idea. Just type Intelli idea on Google
  39. 39. • Download Ultimate.
  40. 40. COMMANDS
  41. 41. The db Command To check your currently selected database, use the command db SYNTAX db EXAMPLE: db
  42. 42. The USE Command To create database in MONGODB we use this command Syntax: Use Database_name
  43. 43. The SHOW Command SYNTAX show dbs EXAMPLE: >show dbs local 0.78125 GB test 0.23012 GB
  44. 44. We created database (Movies) is not present in list. To display database, we need to insert at least one document into it. So to insert something in database we have to use insert command
  45. 45. Example db.movies.insert({”name”:”dead pool”}) WriteResult({ "nInserted" : 1 }) Show dbs cinema 0.000GB local 0.000GB test 0.000GB
  46. 46. Drop database collection Syntax db.dropDatabase Example Use cinema db.dropDatabase Show dbs
  47. 47. The Create collection method Syntax db.createCollection(name, options) “name” is name of collcetion and “options” is a document and is used to specify configuration of collection. (“options” parameter is optional so we need only to give name)
  48. 48. Example Use football db.createCollection(“players”) To show collections use command Show collections
  49. 49. The drop() Method Syntax db.collectionName.drop() Example db.players.drop()
  50. 50. To insert in collection Syntax db.COLLECTION_NAME.insert(document)
  51. 51. Example db.players.insert({ Name:'Cristiano Ronaldo', country:'Portugal', club: 'Real Madrid', Height: 1.85, DOB:5/02/1985 })
  52. 52. The find and pretty method SYNTAX db.collection_name.find() db.collection_name.find().pretty() EXAMPLE db.players.find() db.players.find().pretty()
  53. 53. AND/OR in MongogDB SYNTAX db.mycol.find( { $and: [ {key1: value}, {key2: value2} ]}).pretty()
  54. 54. Example db.players.find({$and:[{“country”: ”Spain”},{“club”: “Real Madrid”}]}) OR command db.players.find({$or:[{“country”: ”Spain”},{“club”: “Real Madrid”}]})
  55. 55. The query of documents depends on some condition Operation syntax example RDBMS equivalence Equality {<key>:<value>} Db.players.find({“c ountry”:”Brazil”}). pretty() Where country=‘Brazil Less than {<key>:{$lt:<value >}} Db.players.find({“ Height”:{$lt:1.8}}.p retty() Where Height<1.9 Greater than {<key>:{$gt:<value >}} Db.players.find({“ Height”:{$gt:1.8}}.p retty() Where Height>1.8 Not equal {<key>:{$ne:<value >}} Db.players.find({“c ountry”:{$ne:”Brazi l”}} Where Country!=Brazil
  56. 56. Example db.players.find({“height”{$gt:1.8}}) db.players.find({“height”{$lt:1.8}})
  57. 57. MongoDB update method SYNTAX db.COLLECTION_NAME.update(selection criteria,update value) EXAMPLE db.players.update({'Name':'Sergio Ramos'},{$set:{'Name':'cavini'}})
  58. 58. If you want to update in multi documents then we can use this command: db.players.update({'Name':'Sergio Ramos'},{$set:{'Name':'cavini'}}) ,{multi:true})
  59. 59. MongoDb save method Syntax db.COLLECTION_NAME.save({_id:ObjectId(),NEW _DATA}) Eaxmple db.mycol.save( { "_id" : ObjectId(5983548781331adf45ec7), “Name":“Cristiano Ronaldo", “Goals": 100 }
  60. 60. Remove method SYNTAX db.COLLECTION_NAME.remove(DELLETION_CRITTERIA) Example db.players.remove({"Name": "Cristiano Ronaldo"}) If you want to delete whole document: db.players.remove()
  61. 61. Different use command “find” SYNTAX db.COLLECTION_NAME.find({},{KEY:1}) EXAMPLE db.players.find({},{_id:0,"Name":1})
  62. 62. Thr limit method SYNTAX db.COLLECTION_NAME.find().limit(NUMBER) EXAMPLE db.players.find({},{_id:0,"Name":1}).limit(5)
  63. 63. MongoDB Skip() Method SYNTAX db.COLLECTION_NAME.find().limit(NUMBER).skip(NUM BER) EXAMPLE db.players.find({},{_id:0,"Name":1}).limit(5).skip(3)
  64. 64. THANK YOU

×