Mongo db basics


Published on

MongoDB basics workshop

Published in: Technology
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Mongo db basics

  1. 1. MongoDB - BasicsApr - 2013cm@paybook.comFriday, May 24, 13
  2. 2. OverviewMongoDB is a document database that provides high performance, highavailability, and easy scalability.• Document Database◦ Documents (objects) map nicely to programming language data types.◦ Embedded documents and arrays reduce need for joins.◦ Dynamic schema makes polymorphism easier.• High Performance◦ Embedding makes reads and writes fast.◦ Indexes can include keys from embedded documents and arrays.◦ Optional streaming writes (no acknowledgments).• High Availability◦ Replicated servers with automatic master failover.• Easy Scalability◦ Automatic sharding distributes collection data across machines.◦ Eventually-consistent reads can be distributed over replicated serversFriday, May 24, 13
  3. 3. A MongoDB deployment hosts a number of databases.A database holdsa set of collections.A collection holds a set of documents.A document isa set of key-value pairs. Documents have dynamic schema. Dynamicschema means that documents in the same collection do not need tohave the same set of fields or structure, and common fields in acollection’s documents may hold different types of data.Mongo Data ModelDatabasesCollectionsDocumentsHostFriday, May 24, 13
  4. 4. QueriesLike others databases engines MongoDBsupports different kind of queries.• Find• Insert• Update• DeleteFriday, May 24, 13
  5. 5. DeploymentMongoDB has the installing capability with the followingconfiguration.• Standalone (Single-instance).• Replica sets, high performance replication withautomated failover.• Shared clusters, partition large data sets overmany machines, transparently to the users(horizontal scalability)Friday, May 24, 13
  6. 6. Technical Facts• MongoDB is a server process that runs on Linux,Windows andOS X. It can be run both as a 32 or 64-bit application.Werecommend running in 64-bit mode, since MongoDB is limitedto a total data size of about 2GB for all databases in 32-bitmode.• The MongoDB process listens on port 27017 by default.• Clients connect to the MongoDB process, optionally authenticatethemselves if security is turned on.• MongoDB stores its data in files (default location is /data/db/),and uses memory mapped files for data management forefficiency.Friday, May 24, 13
  7. 7. NoSQLA NoSQL database provides a mechanism for storage and retrievalof data that use looser consistency models than traditional relationaldatabases in order to achieve horizontal scaling and higher availability.• Features.• Dynamic Schemas (Schemaless).• Auto-replication and sharding.• Integrated cache• SpeedFriday, May 24, 13
  8. 8. NoSQL MongoDBFriday, May 24, 13
  9. 9. Key–value stores allow the application to store its data in a schema-less way.The data could be stored in a datatype of a programminglanguage or an object. Because of this, there is no need for a fixeddata model.A schemaless database allows any data, structured with individualfields and structures, to be stored in the database.Being schemaless reduces ceremony (you dont have to defineschemas) and increases flexibility (you can store all sorts of datawithout prior definition).Schema-lessFriday, May 24, 13
  10. 10. Pros• The data structures that you design on your programming languageare mapped into the database.• ALTER TABLE commands are no longer required.Cons• Not designing the data model schema at the first stage can lead toweak or inconsistent data model in later stage of the application.• You may eventually duplicate the schema across applications.Schema-lessFriday, May 24, 13
  11. 11. JavaScript• JavaScript (JS) is an interpreted computer programminglanguage. It was originally implemented as part of webbrowsers so that client-side scripts could interact with theuser, control the browser, communicate asynchronously, andalter the document content that was displayed.• JavaScripts use in applications outside of web pages—forexample, in PDF documents, site-specific browsers, and desktopwidgets—is also significant. Newer and faster JavaScriptVMs andframeworks built upon them (notably Node.js) have alsoincreased the popularity of JavaScript for server-side webapplications.Friday, May 24, 13
  12. 12. JavaScript & MongoDB• In earlier versions was based on SpiderMonkey engine.• No in 2.4 version MongoDB is based on a JavaScript enginecalledV8.• Improved concurrency for JavaScript operations,• Modernized JavaScript implementation, and• Removed non-standard SpiderMonkey featuresFriday, May 24, 13
  13. 13. JSON• JavaScript Object Notation• Is a lightweight data-interchange format.• It is easy for humans to read and write.• It is easy for machines to parse and generate.• Is a text format that is completely language independent but usesconventions that are familiar to programmers of the C-family oflanguages.Friday, May 24, 13
  14. 14. JSONJSON is built on two structures:• A collection of name/value pairs. In various languages, this is realized as anobject, record, struct, dictionary, hash table, keyed list, or associative array.• An ordered list of values. In most languages, this is realized as an array,vector, list, or sequence.Friday, May 24, 13
  15. 15. JSONObject ArrayFriday, May 24, 13
  16. 16. JSONObject/ArraysFriday, May 24, 13
  17. 17. BSONA serialization format used to storedocuments and make remote procedure callsin MongoDB.“BSON” is a portmanteau of thewords “binary” and “JSON”.Think of BSON asa binary representation of JSON (JavaScriptObject Notation) documents. For a detailedspec, see, May 24, 13
  18. 18. BSON Data typesType TypeDouble Regular expressionString JavaScriptObject SymbolArray 32-bit integerBinary data TimestampObject ID 64-bit integerBoolean Min keyDate Max keyNullFriday, May 24, 13
  19. 19. Drivers• Are available for the following languages.C Node.js*C++ PerlC# PHPErlang PythonJava RubyJavaScript Scala* Node.js based on JavaScriptFriday, May 24, 13
  20. 20. Drivers, May 24, 13
  21. 21. Installing MongoDB• Windows -• Mac -• Linux -, May 24, 13
  22. 22. Mongo ShellThe mongo shell is aninteractive JavaScript shell forMongoDB, and is part of allMongoDB distributions.Friday, May 24, 13
  23. 23. Workshop1. Debian linux virtual machine installed on virtualboxenvironment.2. Start the virtual machine3.The data access to the virtual machine isuser: rootpassword: mongodb2013Friday, May 24, 13
  24. 24. Mongo shell data typesMongoDB BSON provide support for additional data types than JSON.Drivers provide native support for these data types in host languagesand the mongo shell also provides several helper classes to support theuse of these data types in the mongo JavaScript shell. See MongoDBExtended JSON for additional information.• Date• ObjectId• NumberLongFriday, May 24, 13
  25. 25. • To access to Mongo shell just type “mongo”.Mongo shellFriday, May 24, 13
  26. 26. Mongo shell• To access to Mongo on other host shell justtype “mongo host/database”.Friday, May 24, 13
  27. 27. Mongo shell commandhelpersFriday, May 24, 13
  28. 28. CRUDIn computer programming, create, read, update and delete (CRUD) are the four basicfunctions of persistent storage. Sometimes CRUD is expanded with the words retrieveinstead of read, modify instead of update, or destroy instead of delete. It is alsosometimes used to describe user interface conventions that facilitate viewing,searching, and changing information; often using computer-based forms and reports.The acronym CRUD refers to all of the major functions that are implemented inrelational database applications. Each letter in the acronym can map to a standard SQLstatement and HTTP method:Making full use of HTTP methods, along with other constraints, is considered "RESTful".Friday, May 24, 13
  29. 29. Mongo CRUDOperations• Create• Are those that add new records or documents to a collection in MongoDB.• insert()• update()• Read• Are those that retrieve records or documents from a collection inMongoDB• find()• findOne()Friday, May 24, 13
  30. 30. Mongo CRUDOperations• Update• Are those that modify existing records or documents in aMongoDB collection• update()• Delete• Are those that remove documents from a collection inMongoDB• remove()Friday, May 24, 13
  31. 31. CreateThe insert() method inserts a document or documents into acollection.Consider the following behaviors of the insert() method:• If the collection does not exist, then the insert() method willcreate the collection.• If the document does not specify an _id field, then MongoDBwill add the _id field and assign a unique ObjectId for thedocument before inserting.• If the document specifies a new field, then the insert()method inserts the document with the new field.Friday, May 24, 13
  32. 32. CreateThe insert() method takes one of the following parameters:• document – A document to insert into the collection.db.products.insert( { item: "card", qty: 15 } )This operation inserts a new document into the productscollection with the item field set to card, the qty field set to15, and the _id field set to a unique ObjectId:{ "_id" : ObjectId("5063114bd386d8fadbd6b004"), "item" : "card", "qty" : 15 }Friday, May 24, 13
  33. 33. CreateTo insert a single document, with a custom _id field, includethe _id field set to a unique identifier and pass the documentto the insert() method as follows:db.products.insert( { _id: 10, item: "box", qty: 20 } )This operation inserts a new document in the productscollection with the _id field set to 10, the item field set to box,the qty field set to 20:{ "_id" : 10, "item" : "box", "qty" : 20 }Friday, May 24, 13
  34. 34. CreateTo insert multiple documents, pass an array of documents tothe insert() method as in the following:db.products.insert( [ { _id: 11, item: "pencil", qty: 50, type: "no.2" },{ item: "pen", qty: 20 },{ item: "eraser", qty: 25 } ] )Friday, May 24, 13
  35. 35. CreateThe operation will insert three documents into the productscollection:• A document with the fields _id set to 11, item set to pencil, qtyset to 50, and the type set to no.2.• A document with the fields _id set to a unique objectid, itemset to pen, and qty set to 20.• A document with the fields _id set to a unique objectid, itemset to eraser, and qty set to 25.Friday, May 24, 13
  36. 36. Create{ "_id" : 11, "item" : "pencil", "qty" : 50, "type" : "no.2" }{ "_id" : ObjectId("50631bc0be4617f17bb159ca"), "item" : "pen", "qty" : 20 }{ "_id" : ObjectId("50631bc0be4617f17bb159cb"), "item" : "eraser", "qty" : 25 }Friday, May 24, 13
  37. 37. Exercise1. Use the enoan database2. Execute the following commands.- db.products.insert({item:"card", qty:15});- db.products.insert({_id:10, item:"box", qty:20});- db.products.insert([{ _id:11, item:"pencil", qty:50, type:"no.2"},{item:"pen", qty:20},{item:"eraser", qty:25}]);Friday, May 24, 13
  38. 38. ReadThe command to read data in a collection isfind(query,projection).The syntax to use this command isdb.collection.find()When the collection has a lot of documents thefind automatically split the result in packages of20 documents.Friday, May 24, 13
  39. 39. ReadSometimes we need to count the number ofdocuments on a collection with or withoutquery filter.db.collection.find().count()Friday, May 24, 13
  40. 40. ReadSort documentsdb.collection.find().sort()Limit documentsdb.collection.find().limit()Friday, May 24, 13
  41. 41. ReadFind one documentdb.collection.findOne()Format the resultdb.collection.find().pretty()Friday, May 24, 13
  42. 42. Exercise1. In the enoan database find all the documentson the zips collection.1.1 Connect to enoan database usingmongo shell1.2 Execute:db.zips.find()Friday, May 24, 13
  43. 43. ExerciseFriday, May 24, 13
  44. 44. ReadThe command find has the capability of query documents with acondition. Also has the option to project the results of some keys.The method takes the following parameters.query. Optional. Specifies the selection criteria using queryoperators. Omit the query parameter or pass an empty document(e.g. {}) to return all documents in the collection.projection. Optional. Controls the fields to return, or theprojection.The _id key is always returned you must to ignore thiskey to hidde it.Friday, May 24, 13
  45. 45. ReadSome samples with zips collection.db.zips.find()Return all the information on the collection.db.zips.find({"state":"TX"})Return all the documents where the state is equal to TX (Texas)db.zips.find({"state":"TX"},{"city":1,"state":1})Return all the documents where the state is equal to TX (Texas)Shows only state and city keysFriday, May 24, 13
  46. 46. ReadAlso we can use selection criteria in our queries.$gt. Greater than operator$gte. Greater than or equal operator$lt. Lower than operator$lte. Lower than or equal operatordb.zips.find({"pop":{$gt:100000}})Return all the documents where the pop key is greater than 100,000db.zips.find({"pop":{$lt:5}})Friday, May 24, 13
  47. 47. UpdateThe update() method modifies an existing documentor documents in a collection. By default the update()method updates a single document.To update alldocuments in the collection that match the updatequery criteria, specify the multi option.To insert adocument if no document matches the update querycriteria, specify the upsert option.Friday, May 24, 13
  48. 48. UpdateThe update() method takes the following parameters:• query (document). Specifies the selection criteria for theupdate.The query parameter employs the same query selectorsas used in the db.collection.find() method.• update (document). Specifies the modifications to apply.• upsert (boolean). Optional. Specifies an upsert operation• multi (boolean). Optional. Specifies whether to update multipledocuments that meet the query criteria.Friday, May 24, 13
  49. 49. UpdateSome notes about the upsert• The default value is false.When true, the update() method will updatean existing document that matches the query selection criteria or ifno document matches the criteria, insert a new document with thefields and values of the update parameter and if the update includedonly update operators, the query parameter as well.Some notes about the multi• When not specified, the default value is false and the update() methodupdates a single document that meet the query criteria.• When true, the update() method updates all documents that meetthe query criteria.Friday, May 24, 13
  50. 50. Updateupdate() field operators$inc. increments a value of a field by a specified amount.If the field does not exist, $inc sets the field to thespecified amount. $inc accepts positive and negativeincremental amounts.db.collection.update( { field: value },{ $inc: { field1: amount } } );db.collection.update( { age: 20 }, { $inc: { age: 1 } });Friday, May 24, 13
  51. 51. Updateupdate() field operators$rename. updates the name of a field.The new field namemust differ from the existing field name.{$rename: { <old name1>: <new name1>, <old name2>: <newname2>, ... } }db.students.update( { _id: 1 }, { $rename: { nickname: alias,cell: mobile } } )Friday, May 24, 13
  52. 52. Updateupdate() field operators$set. set a particular valuedb.collection.update( { field: value1 }, { $set: { field1: value2 } } );This statement updates in the document in collectionwhere field matches value1 by replacing the value of thefield field1 with value2.This operator will add thespecified field or fields if they do not exist in thisdocument or replace the existing value of the specifiedfield(s) if they already exist.Friday, May 24, 13
  53. 53. Updateupdate() field operators$unset. deletes a particular fielddb.collection.update( { field: value1 }, { $unset: { field1: "" } } );The above example deletes field1 in collection from documentswhere field has a value of value1.The value of the field in the$unset statement (i.e. "" above) does not impact the operation.If documents match the initial query (e.g. { field: value1 } above)but do not have the field specified in the $unset operation (e.g.field1), then the statement has no effect on the document.Friday, May 24, 13
  54. 54. Exercise1. Update the product collection add 20 to qty only on the itemnamed card.2. Update the product collection change the name of the itembox to boxes.3. Update all the documents on the product collection add thekey status with value 1.4. Update all the documents on the product collection changethe key qty to quantity.Friday, May 24, 13
  55. 55. RemoveThe remove method removes documents from a collection.The remove() method can take the following parameters:• query (document) – Optional. Specifies the deletion criteriausing query operators. Omit the query parameter or pass anempty document (e.g. {} ) to delete all documents in thecollection.• justOne (boolean) – Optional.A boolean that limits thedeletion to just one document.The default value is false. Set totrue to delete only the first result.Friday, May 24, 13
  56. 56. RemoveConsider the following examples of the remove method.The remove() method cantake the following parameters:• To remove all documents in a collection, call the remove method with noparameters:db.products.remove()• To remove the documents that match a deletion criteria, call the remove methodwith the query criteria:db.products.remove( { qty: { $gt: 20 } } )• To remove the first document that match a deletion criteria, call the remove methodwith the query criteria and the justOne parameter set to true or 1:db.products.remove( { qty: { $gt: 20 } }, true )Friday, May 24, 13
  57. 57. Exercise1. Remove the item on the product collection where _id is equalto 10.2. Remove all the items on the zips collection where the pop arelower than or equal 1.Friday, May 24, 13
  58. 58. PHP MongodbFriday, May 24, 13
  59. 59. Python MongodbFriday, May 24, 13
  60. 60. SecurityStrategies for Reducing RiskThe most effective way to reduce risk for MongoDB deployments is to runyour entire MongoDB deployment, including all MongoDB components (i.e.mongod, mongos and application instances) in a trusted environment.Trustedenvironments use the following strategies to control access:• Network filter (e.g. firewall) rules that block all connections from unknownsystems to MongoDB components.• Bind mongod and mongos instances to specific IP addresses to limitaccessibility.• Limit MongoDB programs to non-public local networks, and virtual privatenetworks.Friday, May 24, 13
  61. 61. SecurityStrategies for Reducing RiskYou may further reduce risk by:• Requiring authentication for access to MongoDB instances.• Requiring strong, complex, single purpose authentication credentials.This shouldbe part of your internal security policy but is not currently configurable inMongoDB.• Deploying a model of least privilege, where all users have only the amount ofaccess they need to accomplish required tasks, and no more.• Following the best application development and deployment practices, whichincludes: validating all inputs, managing sessions, and application-level accesscontrol.Friday, May 24, 13
  62. 62. ReplicationDatabase replication ensures redundancy, backup, and automaticfailover. Replication occurs through groups of servers known asreplica sets.A MongoDB replica set is a cluster of mongod instances thatreplicate amongst one another and ensure automated failover.Most replica sets consist of two or more mongod instances withat most one of these designated as the primary and the rest assecondary members. Clients direct all writes to the primary,while the secondary members replicate from the primaryasynchronously.Friday, May 24, 13
  63. 63. ReplicationFriday, May 24, 13
  64. 64. ShardingSharding distributes a single logical database system across acluster of machines.Sharding is MongoDB’s approach to scaling out. Sharding partitionsa collection and stores the different portions on differentmachines.When a database’s collections become too large forexisting storage, you need only add a new machine. Shardingautomatically distributes collection data to the new server.Sharding automatically balances data and load across machines.Sharding provides additional write capacity by distributing thewrite load over a number of mongod instances. Sharding allowsusers to increase the potential amount of data in the working set.Friday, May 24, 13
  65. 65. ShardingFriday, May 24, 13
  66. 66. References• Mongodb• 10gen• JSON• BSON• Mongo DB Drivers• PHP mongodb driver• Python mongodb (Pymongo) driver, May 24, 13