23
Database Management Systems MongoDB

Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Database Management Systems

◼ MongoDB

Page 2: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Document Stores

◼ What is a document?

◼ “The Definitive Guide to MongoDB: A complete guide to dealing with Big Data using MongoDB”, David Hows; Peter Membrey; Eelco Plugge; Tim Hawkins, 2015

Page 3: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Documents

◼ Made up of key-value pairs–Each has a type–Order matters (kind of)

◼ Type sensitive and case sensitive

◼ No duplicate keys allowed

Page 4: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Documents

{"firstname": "Peter","lastname": "Membrey","phone_numbers": [

"+852 1234 5678","+44 1234 565 555"

]}

Page 5: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Collections◼ Collections are groups of documents–Is there a schema?

◼ Sub collections are also allowed

Page 6: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Indexing

◼ Automatically created on ID

◼ Can create your own–Embedded documents–Composite Indexes

Page 7: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Design◼ The database is so flexible–How do we choose a good design?

{"type": "Book","Title": "Definitive Guide to MongoDB: A complete guide to dealing with

Big Data using MongoDB 3rd ed., The","ISBN": "978-1-4842-1183-0","Publisher": "Apress","Author": [

"Hows, David""Plugge, Eelco","Membrey, Peter","Hawkins, Tim ]

}

Page 8: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Design{

"Type": "CD","Artist": "Nirvana","Title": "Nevermind","Genre": "Grunge","Releasedate": "1991.09.24","Tracklist": [

{"Track": "1","Title": "Smells Like Teen Spirit","Length": "5:02"},{"Track": "2","Title": "In Bloom","Length": "4:15"}

]}

Page 9: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Database Structure

Page 10: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Data Types

◼ Null◼ Boolean◼ Integer (careful!)◼ Floating point◼ String◼ Date◼ Regular Expression◼ Javascript code◼ Array◼ Embedded Document

Page 11: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Embedding vs. Referencing{

"Type": "CD","Artist": "Nirvana","Title": "Nevermind","Genre": "Grunge","Releasedate": "1991.09.24","Tracklist": [

{"Track" : "1","Title" : "Smells Like Teen Spirit","Length" : "5:02"},{"Track" : "2","Title" : "In Bloom","Length" : "4:15"}

]}

Page 12: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

ObjectIds

◼ Special type that uniquely identifies each object within a collection

Page 13: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Navigation

use library

show dbs

show collections

Page 14: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Insertion

document = ({"Type": "Book", "Title" : "Definitive Guide to MongoDB 3rd ed., The", "ISBN" : "978-1-4842-1183-0", "Publisher" : "Apress", "Author" : ["Hows, David", "Plugge, Eelco", "Membrey, Peter", "Hawkins, Tim"] } )

db.media.insertOne(document)

db.media.insertOne( { "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind" })

Page 15: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Querying

db.media.find()

db.media.find ( { Artist : "Nirvana" } )

db.media.find ( {Artist : "Nirvana"}, {Title: 1} )

db.media.find( { "Author" : "Membrey, Peter" } )

db.media.find().sort( { Title: 1 })

Page 16: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Aggregatesdb.media.count()

db.media.find( { Publisher : "Apress", Type: "Book" } ).count()

db.media.group ({

key: {Title : true},initial: {Total : 0},reduce : function (items,prev){

prev.Total += 1}

})

Page 17: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Conditionals

dvd = ( { "Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Cast" : ["Keanu Reeves","Carrie-Anne Moss","LaurenceFishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"] } )db.media.insertOne(dvd)dvd = ( { "Type" : "DVD", Title : "Blade Runner", Released : 1982 } )db.media.insertOne(dvd)dvd = ( { "Type" : "DVD", Title : "Toy Story 3", Released : 2010 } )db.media.insertOne(dvd)

Page 18: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Conditionals

db.media.find ( { Released : {$gt : 2000} }, { "Cast" : 0 } )

db.media.find ( { Released : {$gte : 1999 } }, { "Cast" : 0 } )

db.media.find ( { Released : {$lt : 1999 } }, { "Cast" : 0 } )

db.media.find( {Released : {$in : [1999,2008,2009] } }, { "Cast" : 0 } )

db.media.find({ $or : [ { "Title" : "Toy Story 3" }, { "ISBN" :"978-1-4842-1183-0" } ] } )

Page 19: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Updatesdb.media.updateOne( { "Title" : "Matrix, The"}, {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action"}, { upsert: true} )

db.media.updateMany( { "Title" : "Matrix, The"}, {$set: {"Type" : "DVD", "Title" : "Matrix, The", "Released" : 1999, "Genre" : "Action"} }, {upsert: true} )

db.media.update ( { "Title" : "Matrix, The" }, {$set : { Genre :"Sci-Fi" } } )

db.media.updateOne ( {"Title": "Matrix, The"}, {$unset : { "Genre" : 1 } } )

Page 20: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Deletion

db.newname.deleteOne( { "Title" : "Different Title" } )

db.newname.deleteMany({})

db.newname.drop()

db.dropDatabase()

Page 21: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

ExerciseThe “videogames” database has a collection called “nintendo” with

documents that look like the following:

{

Title: The Legend of Zelda

Year: 1986

Character: {

Name: Link

Age: 12

}

Ratings: [ 10, 9, 8, 9 ]

}

Page 22: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Exercise

Write a query that will retrieve the document for the game titled “Bubble Bobble”

Write a query that will retrieve the titles (and only the titles) for all games that have Link as a character

Write a query that will retrieve all games that have at least one rating of 9 or greater

Page 23: Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews

Exercises◼ Design a schema to be used to hold users and their reviews of various books. Create a few fake users and reviews and put them in the DB.

◼ Using your schema can you find:–The average review of a book?–The average review of a user?–The number of books in the system?