Mongo Database (Intermediate) Database Systems CSCI-3343 Dr. Tom Hicks Computer Science Department
Execute Each & Every Query As We Go!
Create Backup Directory
Create A Folder Called "C:\Mongo Backups"
Create A Folder Called "C:\Mongo Backups\Scripts"
Add Our Scripts
Make "C:\Mongo Backups" Your Current Directory Now Start Mongo!
Lots Of Review
Execute Each & Every Query As We Go!
List Databases
How Do We List The Databases show dbs
Set The Default Database
How Do We Make military The Default Database use military
Mongo Terminology
The Equivalent Of Tables In Mongo Is _?_ Collections How Do You List The Collections In The Default Database
The equivalent of record/tuple/row, in Mongo, are _?_ Documents
_?_ is a 12 bytes hexadecimal number which assures the uniqueness of every document. _id
Mongo Searching
You probably have some consultants left over from lecture last time You probably have some consultants left over from lecture last time. List all of the information about all of the collections. db.consultant.find() Do any of you have this? db.consultant.find({}) Do any of you have this?
How can you show all of the information about consultant Po? db.consultant.find({name: "Po"}) db.consultant.find({rank: "Colonel"}) May Have More Than Po!
Delete A Document
How can you delete the consultant Po? db.consultant.remove({name: "Po"})
Adding A Document
Add Consultant Mark General db.consultant.insert({name: "Mark", rank: "General"})
Add Consultant Paul db.consultant.insert({name: "Paul"})
Documents In Sorted Order
List All Of The Consultants In Order By Name db.consultant.find().sort({name:1})
Do All Documents Have To Have The Same Fields?
Do You Think This Query Will Work? No Name No Rank ? You Don't Need To Do This One!
It Appears To Work?
List All Of The Consultants In Order By Name db.consultant.find().sort({name:1})
Display With A Nice Layout
List All Of The Consultants In Order By Name Better Way To Render The Jason Scripts? db.consultant.find().sort({name:1}).pretty()
Advantages Of Mongo Database
Mongo Advantages Over RDBMS -1 Schema less: MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another. Structure of a single object is clear. No complex joins. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
Mongo Advantages Over RDBMS - 2 Ease of scale-out: MongoDB is easy to scale. Conversion/mapping of application objects to database objects not needed. Uses internal memory for storing the (windowed) working set, enabling faster access of data.
File NewConsultants.txt
Create NewConsultants.txt
Why Use Mongo?
Why Use Mongo? Document Oriented Storage: Data is stored in the form of JSON style documents. Index on any attribute Replication and high availability Auto-sharding Rich queries Fast in-place updates "Sharding" is a method for distributing data across multiple machines
Remove All Items From A Collection
How can you delete all of the consultants, but not the collection" db.consultant.remove({})
Batch Process A Set Of Queries
Perform These Queries
Display Only Some Of The Document Fields
List The Name & Email Of All Of The Consultants In Order By Name db.consultant.find({}, {name:1, email:1, _id:0}).sort({name:1})
List The Collections
List All Of The Collections In The Military Database show collections
Create A Collection
Create A Collection db.createCollection("Gamers")
Look Through Manals Often Optional Arguments
Backup A Mongo Database
Backup A Mongo Database mongodump --db military several ways to do
Check The Backup?
Delete A Database
Delete the military database db.dropDatabase()
Restore A Database
Restore Database military mongorestore --drop -d military dump\military
Delete A Collection
Delete The consultant Collection db.consultant.drop()
Reload From File NewConsultants.txt
Create NewConsultants.txt
Display Only Some Of The Document Fields
List The name & mid Of All Of The Consultants db.consultant.find({}, {name:1, mid:1, _id:0})
Count
List The Number Of Consultants db.consultant.find({}).count()
Search Query Greater Than
List The name & mid Of Those Consultants Whose mid > 1013 In Order By name db.consultant.find({mid: {$gt: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})
List The The Number Of Consultants Whose mid > 1013 db.consultant.find({mid: {$gt: 1013}}).count()
Search Query Greater Than Or Equal To
List The name & mid Of Those Consultants Whose mid >= 1013 In Order By name db.consultant.find({mid: {$gte: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})
List The The Number Of Consultants Whose mid >= 1013 db.consultant.find({mid: {$gte: 1013}}).count()
Search Query Less Than
List The name & mid Of Those Consultants Whose mid < 1006 In Order By mid db.consultant.find({mid: {$lt: 1006}}, {name:1, mid:1, _id:0}).sort({mid: 1})
List The The Number Of Consultants Whose mid < 1006 db.consultant.find({mid: {$lt: 1006}}).count()
Search Query Less Than Or Equal To
List The name & mid Of Those Consultants Whose mid <= 1006 In Order By mid db.consultant.find({mid: {$lte: 1006}}, {name:1, mid:1, _id:0}).sort({mid: 1})
List The The Number Of Consultants Whose mid <= 1006 db.consultant.find({mid: {$lte: 1006}}).count()
Search Query Not Equal To
List The name & mid Of Those Consultants Whose mid <> 1013 In Order By name db.consultant.find({mid: {$ne: 1013}}, {name:1, mid:1, _id:0}).sort({name:1})
List The The Number Of Consultants Whose mid <> 1006 db.consultant.find({mid: {$ne: 1006}}).count()
Search Query Equal To
List The name & mid Of Those Consultants Whose mid = 1013 In Order By name db.consultant.find({mid: 1013}, {name:1, mid:1, _id:0}).sort({name:1})
Search Query AND
List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #1 Solve In Parts When Complex: mid > 1004 db.consultant.find ({mid: {$gte: 1004}}, {_id:0, mid:1})
List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #2 Solve In Parts When Complex: mid > 1004 & captain db.consultant.find ({$and: [{mid: {$gte: 1004}}, {rank: "Captain"}]}, {_id:0, rank:1, mid:1})
List The name, rank, & mid Of Those Consultants Whose mid > 1004 AND Are Capitan In Order By name #3 Solve In Parts When Complex: complete db.consultant.find ({$and: [{mid: {$gte: 1004}}, {rank: "Captain"}]}, {_id:0, name:1, rank:1, mid:1}).sort({name:1})
Search Query OR
List The name, rank, & mid Of Those Consultants Whose Are Capitan or Colonel In Order By name db.consultant.find ({$or: [{rank: "Captain"}, {rank: "Colonel"}]}, {_id:0, name:1, rank:1, mid:1}).sort({name:1})
Search Query Substring
List The Consultant names That Have An 'a' In Their Name db.consultant.find({name: {$regex: "a"}}, {_id:0, name:1})
List The names Of All Consultant names That Have An 'an' In Their Name db.consultant.find({name: {$regex: 'an'}}, {_id:0, name:1})
List The names Of All Consultant names That Have An 'ie' In Their Name db.consultant.find({name: {$regex: "ie"}}, {_id:0, name:1})
List The names Of All Consultant names That Have An 'C/c' In Their Name db.consultant.find({$or: [{name: {$regex: "C"}}, {name: {$regex: "c"}}]}, {_id:0, name:1})
Search Query Case Insensitive Search
List The Consultant names That Match miCHaeL Case Insensitive db.consultant.find({name: /miCHaeL/i}, {_id:0, name: 1})
List The Consultant names That Contain C/c Case Insensitive db.consultant.find({name: /c/i}, {_id:0, name: 1})
List The Consultant names That Contain C/c Case Insensitive db.consultant.find({name: /.c/i}, {_id:0, name: 1})
Mongo Datatypes
Mongo Has Many Datatypes #1 String: This is the most commonly used datatype to store the data. String in MongoDB must be UTF-8 valid. Integer: This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. Boolean: This type is used to store a boolean (true/ false) value. Double: This type is used to store floating point values. Min/Max Keys: This type is used to compare a value against the lowest and highest BSON elements.
Mongo Has Many Datatypes #2 Arrays: This type is used to store arrays or list or multiple values into one key. Timestamp: ctimestamp. This can be handy for recording when a document has been modified or added. Object: This datatype is used for embedded documents. Null: This type is used to store a Null value. Symbol: This datatype is used identically to a string; however, it's generally reserved for languages that use a specific symbol type.
Mongo Has Many Datatypes #3 Date: This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it. Object ID: This datatype is used to store the document’s ID. Binary Data: This datatype is used to store binary data. Code: This datatype is used to store JavaScript code into the document. Regular Expression: This datatype is used to store regular expression.
Update Query A Single Document
Promote consultant Evan To General db.consultant.update({name: "Evan"}, {$set: {rank: "General"}})
Change Brent's mid to 1015 db.consultant.update({name: "Brent"}, {$set: {mid: 1015}})
Update Increment Query
Increment consultant Evan's mid by 1 db.consultant.update({name: "Evan"}, {$inc: {mid: 1}})
Increment consultant Evan's mid by 3 db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})
Decrement consultant Evan's mid by Increment by -4 db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})
Update Multiply Query
Double consultant Samuel mid db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}})
Half consultant Samuel mid db.consultant.update({name: "Samuel"}, {$mul: {mid: .5}})
Update Query Multiple Documents
Double mid For All consultants db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}}, {multi: true})
Replace Document Query
Replace consultant Tom With Tom - Private db.consultant.save({_id : ObjectId("583a1ba051ae2637bd82b573"), name: "Tom", rank: "Private", wife: "Sherry"}) By ObjectID! If No ObjectID match Does Insert
Create File NewerConsultant.txt
Create File NewerConsultants.txt Replace Collection consultant
Set No To Display DBQuery.shellBatchSize
Display Name, Rank, Deleted For All it!
Set The Number To Display (without IT) DBQuery.shellBatchSize = 100
Ordered Data Query
List Name & Rank & Deleted for All Consultants Order By Rank db.consultant.find ({}, {_id:0, name:1, rank:1}).sort({rank:1})
List Name & Rank & Deleted for All Consultants Order By Rank By Name db.consultant.find ({}, {_id:0, name:1, rank:1}).sort({rank:1, name:1})
Query View
View #1 : all consultants in order by name skip _id field db.consultant.find ({}, {_id:0, name:1, mid:1, rank:1, server:1, email:1, deleted:1}).sort({name:1})
View #2 : how many consultants (total?) db.consultant.find ().count() View #3 : how many deleted consultants db.consultant.find ({deleted: 'T'}).count() View #4 : how many non-deleted consultants db.consultant.find ({deleted: 'F'}).count()
Now Generate The Equivalent View In Mongo RDMS VIEW SELECT * FROM consultants WHERE (Deleted = 'F') ORDER BY name Now Generate The Equivalent View In Mongo
View: All non-deleted consultants in order by name Query? db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1})
Limit Query
Use Limit 4 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1}).Limit(4)
Use Limit 1 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({rank:1, name:1}).Limit(1) As I Mentioned In The Last Set Of Slides, "Limit" accepts only one argument and cannot be used, by itself, to identify the 37th document within our view!
Skipping Query
Run The Following Query db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, email:1}).sort({name:1, rank:1}).skip(5).limit(1) What Is The Significance Of This Query? MySQL Limit 5,1
Drivers: C, C++, C#, .Net, PHP, Python, Scala, Ruby, Pearl, Java, etc.
Mongo Indexing
Index Same Types Of Things That We Do For RDMS
What Data Structure Do You Suppose Mongo Uses For Their Indexing? B+ Tree Since No One Uses B Trees, Everyone Simply Refers To The Structure As A B-Tree
Indexing Is A Critical Part Of All Database Design Indexing Critical! Will Not Matter On Our Simple Databases
Create A consultant Index On name db.consultant.ensureIndex ({name:1})
List The Indexes db.consultant.getIndexes()
Another Way To Create An Index db.consultant.createIndex({mid:1})
Start Robomongo Check Out Your Indexes!
Rebuild Index? Can Take Hours!
Robomongo To Create An Index #1 Please Do This!
Robomongo To Create An Index #2 Please Do This!
Search Query Wild Card Search
Awkward Queries db.consultant.aggregate( [ { $group: { _id: null, total : { $sum: "$mid" } high: { $max: "$mid" } } } ] )