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.

9b. Document-Oriented Databases lab

1,798 views

Published on

In this lecture we analyze document oriented databases. In particular we consider why there are the first approach to nosql and what are the main features. Then, we analyze as example MongoDB. We consider the data model, CRUD operations, write concerns, scaling (replication and sharding).
Finally we presents other document oriented database and when to use or not document oriented databases.

Published in: Data & Analytics
  • Login to see the comments

  • Be the first to like this

9b. Document-Oriented Databases lab

  1. 1. Document Oriented Databases Lab Ciao ciao Vai a fare ciao ciao Dr. Fabio Fumarola
  2. 2. MongoDB 2
  3. 3. MongoDB with Docker • Download docker images: – https://registry.hub.docker.com/_/mongo/ • Run Mongo: – docker run --name mongo -d -p 27017:27017 mongo • Connect to Mongo: – docker exec -it mongo bash • Connect via robomongo: – http://robomongo.org/ 3
  4. 4. Mongodb Fundamentals 4
  5. 5. Starting Mongo • Asking for help: – help 5
  6. 6. Import same data • curl -L http://j.mp/OSCONvenues | mongoimport -d conf -c venues 6
  7. 7. Example of object 7 var place1 = { "name" : "10gen HQ", "address" : "578 Broadway 7th Floor", "city" : "New York", "zip" : "10011", "tags" : [ "business", "awesome" ] }
  8. 8. Insert >use places >db.places.insert(place1) It returns: WriteResult({ "nInserted" : 1 }) 8
  9. 9. Querying > db.places.find() •Specify equality conditions > db.places.find({ zip: "10011", tags: "awesome" }) 9 { "_id" : ObjectId("555473fb37abe83242ddd405"), "name" : "10gen HQ", "address" : "578 Broadway 7th Floor", "city" : "New York", "zip" : "10011", "tags" : [ "business", "awesome" ] }
  10. 10. Nested Documents 10 var place2 = { "name" : "10gen HQ", "address" : "578 Broadway 7th Floor", "city" : "New York", "zip" : "10011", "tags" : [ "business", "awesome" ], "comments" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "author" : "Fabio", "text" : "best place" } ] }
  11. 11. Querying Nested Documents • Find by specifying author – db.places.find({"comments.author" : "Fabio"}) • Greater Than Operator ($gt): – db.places.find({"comments.score" : { $gt: 20}}) • Less Than Operator ($lt) – db.places.find({"comments.score" : { $lt: 20}}) – db.places.find({"comments.score" : { $lt: 40}}) • Conditions can be logically combined 11
  12. 12. Logical Combination • Logical AND – db.places.find({ "name" : "10gen HQ", "comments.author" : "Fabio" }) – db.places.findOne({ "name" : "10gen HQ", "comments.author" : "Fabio" }) • Logical OR – db.places.find({ $or : [{ "name" : "10gen HQ" }, {"comments.author" : "Fabio" }]}) 12
  13. 13. Sort • to sort by and the corresponding sort type: – e.g. 1 for ascending – and -1 for descending. • Example: – db.places.find().sort({"name" : 1, "comments.score" : -1}) 13
  14. 14. Updating • We can use the update() method to update documents of a collection. The method accepts as its parameters: – a filter document to match the documents to update, – an update document to specify the modification to perform, and – an options parameter (optional). 14
  15. 15. Update 15 db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2015, text : "Office hours are great!" } } } )
  16. 16. Update Multi 16 db.places.update( {name : "10gen HQ"}, { $push : { comments : { author : "steve", date : 6/26/2015, text : "Office hours are great!" } } }, { multi: true} )
  17. 17. Remove • We can use the remove() method to remove documents from a collection. • The method takes a conditions document that determines the documents to remove. • To specify a remove condition, 17
  18. 18. Remove Example db.places.remove({"name" : "10gen HQ"}) db.places.remove({"name" : "10gen HQ"}, { justOne: true } ) 18
  19. 19. Drop Collections and DB • The drop() method to drop a collection, including any indexes – db.places.drop() • Drop a db – use test – db.dropDatabase() 19
  20. 20. Advanced Operations 20
  21. 21. Regular Expression 21 // Regular Expressions > db.posts.find({'comments.author': /^Fa/}){ _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), name : "10gen HQ", address : "578 Broadway 7th Floor", city : "New York", zip : "10011", comments : [ { author : ”Fabio", date : "Sat Apr 25 2010 20:51:03",
  22. 22. Cursor • How to get a cursor: – var it = db.venues.find() • Methods – next – hasNext 22
  23. 23. Aggregation • Group Documents by a Field and Calculate Count 23 db.venues.aggregate( [ { $group: { "_id": "$location.city", "count": { $sum: 1 } } } ] )
  24. 24. Indexes • By default index on collections are only on the _id • the createIndex() method is used to create an index on a collection • Indexes can support the efficient execution of queries. – For an ascending index type, specify 1 for <type>. – For a descending index type, specify -1 for <type>. 24
  25. 25. Create Index 25 { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } db.venues.createIndex({"country": 1})
  26. 26. Create a Compound Index 26 db.venues.createIndex({"country": 1, "distance": -1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 2, "numIndexesAfter" : 3, "ok" : 1 } Upon successful index creation, the "numIndexesAfter" value is one greater than the "numIndexesBefore" value.
  27. 27. Geospatial Index 27 db.venues.ensureIndex({ "location.geo": "2d" }) db.venues.find( {"location.geo":{$near:[-122.6600248120735,45.5302342677652]}}) db.venues.find( { "location.geo" : { $geoWithin : { $box : [ [ 0 , 0 ] , [ -122 , 48 ] ] } } } )
  28. 28. References • http://docs.mongodb.org/manual/ • https://gist.github.com/df238517f06500fec317.git 28

×