Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to...
Transcript of Database Management Systemsdshook/cse530/lectures/Document.pdf · Design a schema to be used to...
![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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042200/5e9f2fcf1b626e61f13cde13/html5/thumbnails/23.jpg)
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?