Download presentation
Presentation is loading. Please wait.
1
Mongo Database (Intermediate)
Database Systems CSCI-3343 Dr. Tom Hicks Computer Science Department
2
Execute Each & Every Query As We Go!
3
Create Backup Directory
4
Create A Folder Called "C:\Mongo Backups"
5
Create A Folder Called "C:\Mongo Backups\Scripts"
6
Add Our Scripts
7
Make "C:\Mongo Backups" Your Current Directory
Now Start Mongo!
8
Lots Of Review
9
Execute Each & Every Query As We Go!
10
List Databases
11
How Do We List The Databases
show dbs
12
Set The Default Database
13
How Do We Make military The Default Database
use military
14
Mongo Terminology
15
The Equivalent Of Tables In Mongo Is _?_
Collections How Do You List The Collections In The Default Database
16
The equivalent of record/tuple/row, in Mongo, are _?_
Documents
17
_?_ is a 12 bytes hexadecimal number which assures the uniqueness of every document.
_id
18
Mongo Searching
19
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?
20
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!
21
Delete A Document
22
How can you delete the consultant Po?
db.consultant.remove({name: "Po"})
23
Adding A Document
24
Add Consultant Mark General
db.consultant.insert({name: "Mark", rank: "General"})
25
Add Consultant Paul db.consultant.insert({name: "Paul"})
26
Documents In Sorted Order
27
List All Of The Consultants In Order By Name
db.consultant.find().sort({name:1})
28
Do All Documents Have To Have The Same Fields?
29
Do You Think This Query Will Work?
No Name No Rank ? You Don't Need To Do This One!
30
It Appears To Work?
31
List All Of The Consultants In Order By Name
db.consultant.find().sort({name:1})
32
Display With A Nice Layout
33
List All Of The Consultants In Order By Name Better Way To Render The Jason Scripts?
db.consultant.find().sort({name:1}).pretty()
34
Advantages Of Mongo Database
35
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.
36
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.
37
File NewConsultants.txt
38
Create NewConsultants.txt
39
Why Use Mongo?
40
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
41
Remove All Items From A Collection
42
How can you delete all of the consultants, but not the collection"
db.consultant.remove({})
43
Batch Process A Set Of Queries
44
Perform These Queries
45
Display Only Some Of The Document Fields
46
List The Name & Email Of All Of The Consultants In Order By Name
db.consultant.find({}, {name:1, 1, _id:0}).sort({name:1})
47
List The Collections
48
List All Of The Collections In The Military Database
show collections
49
Create A Collection
50
Create A Collection db.createCollection("Gamers")
51
Look Through Manals Often Optional Arguments
52
Backup A Mongo Database
53
Backup A Mongo Database
mongodump --db military several ways to do
54
Check The Backup?
55
Delete A Database
56
Delete the military database
db.dropDatabase()
57
Restore A Database
58
Restore Database military
mongorestore --drop -d military dump\military
59
Delete A Collection
60
Delete The consultant Collection
db.consultant.drop()
61
Reload From File NewConsultants.txt
62
Create NewConsultants.txt
63
Display Only Some Of The Document Fields
64
List The name & mid Of All Of The Consultants
db.consultant.find({}, {name:1, mid:1, _id:0})
65
Count
66
List The Number Of Consultants
db.consultant.find({}).count()
67
Search Query Greater Than
68
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})
69
List The The Number Of Consultants Whose mid > 1013
db.consultant.find({mid: {$gt: 1013}}).count()
70
Search Query Greater Than Or Equal To
71
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})
72
List The The Number Of Consultants Whose mid >= 1013
db.consultant.find({mid: {$gte: 1013}}).count()
73
Search Query Less Than
74
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})
75
List The The Number Of Consultants Whose mid < 1006
db.consultant.find({mid: {$lt: 1006}}).count()
76
Search Query Less Than Or Equal To
77
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})
78
List The The Number Of Consultants Whose mid <= 1006
db.consultant.find({mid: {$lte: 1006}}).count()
79
Search Query Not Equal To
80
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})
81
List The The Number Of Consultants Whose mid <> 1006
db.consultant.find({mid: {$ne: 1006}}).count()
82
Search Query Equal To
83
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})
84
Search Query AND
85
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})
86
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})
87
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})
88
Search Query OR
89
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})
90
Search Query Substring
91
List The Consultant names That Have An 'a' In Their Name
db.consultant.find({name: {$regex: "a"}}, {_id:0, name:1})
92
List The names Of All Consultant names That Have An 'an' In Their Name
db.consultant.find({name: {$regex: 'an'}}, {_id:0, name:1})
93
List The names Of All Consultant names That Have An 'ie' In Their Name
db.consultant.find({name: {$regex: "ie"}}, {_id:0, name:1})
94
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})
95
Search Query Case Insensitive Search
96
List The Consultant names That Match miCHaeL Case Insensitive
db.consultant.find({name: /miCHaeL/i}, {_id:0, name: 1})
97
List The Consultant names That Contain C/c Case Insensitive
db.consultant.find({name: /c/i}, {_id:0, name: 1})
98
List The Consultant names That Contain C/c Case Insensitive
db.consultant.find({name: /.c/i}, {_id:0, name: 1})
99
Mongo Datatypes
100
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.
101
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.
102
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.
103
Update Query A Single Document
104
Promote consultant Evan To General
db.consultant.update({name: "Evan"}, {$set: {rank: "General"}})
105
Change Brent's mid to 1015 db.consultant.update({name: "Brent"}, {$set: {mid: 1015}})
106
Update Increment Query
107
Increment consultant Evan's mid by 1
db.consultant.update({name: "Evan"}, {$inc: {mid: 1}})
108
Increment consultant Evan's mid by 3
db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})
109
Decrement consultant Evan's mid by Increment by -4
db.consultant.update({name: "Evan"}, {$inc: {mid: 3}})
110
Update Multiply Query
111
Double consultant Samuel mid
db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}})
112
Half consultant Samuel mid
db.consultant.update({name: "Samuel"}, {$mul: {mid: .5}})
113
Update Query Multiple Documents
114
Double mid For All consultants
db.consultant.update({name: "Samuel"}, {$mul: {mid: 2}}, {multi: true})
115
Replace Document Query
116
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
117
Create File NewerConsultant.txt
118
Create File NewerConsultants.txt
Replace Collection consultant
119
Set No To Display DBQuery.shellBatchSize
120
Display Name, Rank, Deleted For All it!
121
Set The Number To Display (without IT)
DBQuery.shellBatchSize = 100
122
Ordered Data Query
123
List Name & Rank & Deleted for All Consultants Order By Rank
db.consultant.find ({}, {_id:0, name:1, rank:1}).sort({rank:1})
124
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})
125
Query View
126
View #1 : all consultants in order by name skip _id field
db.consultant.find ({}, {_id:0, name:1, mid:1, rank:1, server:1, 1, deleted:1}).sort({name:1})
127
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()
128
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
129
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, 1}).sort({rank:1, name:1})
130
Limit Query
131
Use Limit 4 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, 1}).sort({rank:1, name:1}).Limit(4)
132
Use Limit 1 In Our View db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, 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!
133
Skipping Query
134
Run The Following Query
db.consultant.find ({deleted: 'F'}, {_id:0, name:1, mid:1, rank:1, server:1, 1}).sort({name:1, rank:1}).skip(5).limit(1) What Is The Significance Of This Query? MySQL Limit 5,1
135
Drivers: C, C++, C#, .Net, PHP, Python, Scala, Ruby, Pearl, Java, etc.
136
Mongo Indexing
137
Index Same Types Of Things
That We Do For RDMS
138
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
139
Indexing Is A Critical Part Of All Database Design
Indexing Critical! Will Not Matter On Our Simple Databases
140
Create A consultant Index On name
db.consultant.ensureIndex ({name:1})
141
List The Indexes db.consultant.getIndexes()
142
Another Way To Create An Index
db.consultant.createIndex({mid:1})
143
Start Robomongo Check Out Your Indexes!
144
Rebuild Index? Can Take Hours!
145
Robomongo To Create An Index #1
Please Do This!
146
Robomongo To Create An Index #2
Please Do This!
147
Search Query Wild Card Search
148
Awkward Queries db.consultant.aggregate( [ { $group: { _id: null, total : { $sum: "$mid" } high: { $max: "$mid" } } } ] )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.