-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic Assignment.txt
138 lines (86 loc) · 6.25 KB
/
Basic Assignment.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
>use mongo_practice
db.movies.insert({title:"Fight Club", writer: "Chuck Palahniuko", year: "1999", actors:["Brad Pitt", "Edward Norton"]})
db.movies.insert({title:"Pulp Fiction", writer:"Quentin Tarantino", year:"1994", actors:["John Travolta", "Uma Thurman"]})
db.movies.insert({title:"Inglorious Basterds", writer:"Quentin Tarantino", year:"2009", actors:["Brad Pitt", "Diane Kruger", "Eli Roth"]})
db.movies.insert({title:"The Hobbit: An unexpected Journey", writer:"J.R.R. Tolkein", year:"2013",franchise:"The Hobbit"})
db.movies.insert({title:"The Hobbit: The Desolation of Smaug", writer:"J.R.R Tolkien", year:"2013", franchise:"The Hobbit"})
db.movies.insert({title:"The Hobbit: The Battle of the Five Armies", writer:"J.R.R Tolkien", year:"2012", franchise:"The Hobbit", synopsis:"Bilbo and Company are forced to engage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness."})
db.movies.insert({title:"Pee Wee Herman's Big Adventures"})
db.movies.insert({title:"Avatar"})
Query / Find Documents
query the movies collection to
* to get all documents
db.movies.find()
*get all documents with writer set to "Quentin Tarantino"
db.movies.find(writer:"Quentin Tarantino")
*get all documents where actors include "Brad Pitt
db.movies.find({actors: "Brad Pitt"})
*get all documents with franchise set to "The Hobbit"
db.movies.find({franchise:"The Hobbit"})
*get all movies released in the 90s
db.movies.find({year: {$gt:"1990", $lt:"2000"}})
*get all movies released before the year 2000 or after 2010
db.movies.find({$or:[{year:{$gt:"2010"}},{year: {$lt:"2000"}}]})
Update Documents
1.add a synopsis to "The Hobbit: An Unexpected Journey" : "A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."
>db.movies.update({movie:"The Hobbit: An Unexpected Journey"}, {$set: {synopsis:"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
Or
>db.movies.update({_id:ObjectId("6002ab597e2b847c9d89cfca"")}, {$set:{synopsis:"A reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain with a spirited group of dwarves to reclaim their mountain home - and the gold within it - from the dragon Smaug."}})
2.add a synopsis to "The Hobbit: The Desolation of Smaug" : "The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."
db.movies.update({movie:"The Hobbit: The Desolation of Smaug"}, {$set:{synopsis:"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
Or
db.movies.update({_id:ObjectId("6002ab7b7e2b847c9d89cfcb")}, {$set:{synopsis:"The dwarves, along with Bilbo Baggins and Gandalf the Grey, continue their quest to reclaim Erebor, their homeland, from Smaug. Bilbo Baggins is in possession of a mysterious and magical ring."}})
3.add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"
db.movies.update({movie:"Pulp Fiction"},{$set:{actors:"Samuel L. Jackson"}})
Or
db.movies.update({_id:ObjectId("6002ab467e2b847c9d89cfc8")}, {$push:{actors:"Samuel L. Jackson"}})
Text Search
1.find all movies that have a synopsis that contains the word "Bilbo"
db.movies.find({synopsis:{$regex:"Bilbo"}})
2.find all movies that have a synopsis that contains the word "Gandalf"
db.movies.find({synopsis:{$regex:"Gandalf"}})
3.find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
db.movies.find({$and:[{synopsis:{$regex:"Bilbo"}}, {synopsis:{$not:/Gandalf/}}]})
4.find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
db.movies.find({$or:[{synopsis:{$regex:"dwarves"}}, {synopsis:{$regex:"hobbit"}}]})
5.find all movies that have a synopsis that contains the word "gold" and "dragon"
db.movies.find({$and:[{synopsis:{$regex:"gold"}}, {synopsis:{$regex:"dragon"}}]})
Delete Documents
1.delete the movie "Pee Wee Herman's Big Adventure"
db.movies.remove({movie:"Pee Wee Herman's Big Adventure"})
Or
db.movies.remove({_id:ObjectId("6002ab927e2b847c9d89cfcd")})
2.delete the movie "Avatar"
db.movies.remove({movie:"Avatar"})
Or
db.movies.remove({_id:ObjectId("6002ab9c7e2b847c9d89cfce")})
Relationships
Insert the following documents into a users collection
db.users.insert({username:"GoodGuyGreg", first_name:"Good Guy", last_name:"Greg"})
db.users.insert({username:"Scumbag Steve", fullname:{first: "Scumbag", last:"Steve"}})
Insert the following documents into a posts collection
db.posts.insert({username:"GoodGuyGreg", title:"Passes out at Party", body:"Wakes up early and cleans house"})
db.posts.insert({ username:"GoodGuyGreg", title:"Steals your identity", body:"Raises your credit score"})
db.posts.insert({username:"GoodGuyGreg", title:"Reports a bug in your code", body:"Sends you a pull request"})
db.posts.insert({ username:"ScumbagSteve", title:"Borrows something", body:"Sells it"})
db.posts.insert({ username:"ScumbagSteve", title:"Borrows everything", body:"The end"})
db.posts.insert({username:"ScumbagSteve", title:"Forks your repo on github", body:"Sets to private"})
Insert the following documents into a comments collection
(need to insert)
Querying related collections
1.find all users
db.users.find().pretty()
2.find all posts
db.posts.find().pretty()
3.find all posts that was authored by "GoodGuyGreg"
db.posts.find({username:"GoodGuyGreg"})
4.find all posts that was authored by "ScumbagSteve"
db.posts.find({username:"ScumbagSteve"})
5.find all comments
db.comments.find().pretty()
6.find all comments that was authored by "GoodGuyGreg"
db.comments.find({username:"GoodGuyGreg"})
7.find all comments that was authored by "ScumbagSteve"
db.comments.find({username:"ScumbagSteve"})
8.find all comments belonging to the post "Reports a bug in your code"
db.comments.find({title:"Reports a bug in your code"})