Skip to content

Instantly share code, notes, and snippets.

@ssi-anik
Last active May 23, 2022 22:17
Show Gist options
  • Save ssi-anik/71b49be64c7d9724ec863ced9b4e750a to your computer and use it in GitHub Desktop.
Save ssi-anik/71b49be64c7d9724ec863ced9b4e750a to your computer and use it in GitHub Desktop.
MongoDB exercise: https://gist.github.com/theRemix/7305403e1ab6fc8674f0 (Original GIST)

MongoDB Practice

MongoDB Exercise in mongo shell

Connect to a running mongo instance, use a database named mongo_practice.

Document all your queries in a javascript file to use as a reference.

Insert Documents

Insert the following documents into a movies collection.

title : Fight Club
writer : Chuck Palahniuk
year : 1999
actors : [
  Brad Pitt
  Edward Norton
]
title : Pulp Fiction
writer : Quentin Tarantino
year : 1994
actors : [
  John Travolta
  Uma Thurman
]
title : Inglorious Basterds
writer : Quentin Tarantino
year : 2009
actors : [
  Brad Pitt
  Diane Kruger
  Eli Roth
]
title : The Hobbit: An Unexpected Journey
writer : J.R.R. Tolkein
year : 2012
franchise : The Hobbit
title : The Hobbit: The Desolation of Smaug
writer : J.R.R. Tolkein
year : 2013
franchise : The Hobbit
title : The Hobbit: The Battle of the Five Armies
writer : J.R.R. Tolkein
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.
title : Pee Wee Herman's Big Adventure
title : Avatar

Query / Find Documents

query the movies collection to

  1. get all documents
  2. get all documents with writer set to "Quentin Tarantino"
  3. get all documents where actors include "Brad Pitt"
  4. get all documents with franchise set to "The Hobbit"
  5. get all movies released in the 90s
  6. get all movies released before the year 2000 or after 2010

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."
  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."
  3. add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"

Text Search

  1. find all movies that have a synopsis that contains the word "Bilbo"
  2. find all movies that have a synopsis that contains the word "Gandalf"
  3. find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
  4. find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
  5. find all movies that have a synopsis that contains the word "gold" and "dragon"

Delete Documents

  1. delete the movie "Pee Wee Herman's Big Adventure"
  2. delete the movie "Avatar"

Relationships

Insert the following documents into a users collection

username : GoodGuyGreg
first_name : "Good Guy"
last_name : "Greg"
username : ScumbagSteve
full_name :
  first : "Scumbag"
  last : "Steve"

Insert the following documents into a posts collection

username : GoodGuyGreg
title : Passes out at party
body : Wakes up early and cleans house
username : GoodGuyGreg
title : Steals your identity
body : Raises your credit score
username : GoodGuyGreg
title : Reports a bug in your code
body : Sends you a Pull Request
username : ScumbagSteve
title : Borrows something
body : Sells it
username : ScumbagSteve
title : Borrows everything
body : The end
username : ScumbagSteve
title : Forks your repo on github
body : Sets to private

Insert the following documents into a comments collection

username : GoodGuyGreg
comment : Hope you got a good deal!
post : [post_obj_id]

where [post_obj_id] is the ObjectId of the posts document: "Borrows something"

username : GoodGuyGreg
comment : What's mine is yours!
post : [post_obj_id]

where [post_obj_id] is the ObjectId of the posts document: "Borrows everything"

username : GoodGuyGreg
comment : Don't violate the licensing agreement!
post : [post_obj_id]

where [post_obj_id] is the ObjectId of the posts document: "Forks your repo on github

username : ScumbagSteve
comment : It still isn't clean
post : [post_obj_id]

where [post_obj_id] is the ObjectId of the posts document: "Passes out at party"

username : ScumbagSteve
comment : Denied your PR cause I found a hack
post : [post_obj_id]

where [post_obj_id] is the ObjectId of the posts document: "Reports a bug in your code"

Querying related collections

  1. find all users
  2. find all posts
  3. find all posts that was authored by "GoodGuyGreg"
  4. find all posts that was authored by "ScumbagSteve"
  5. find all comments
  6. find all comments that was authored by "GoodGuyGreg"
  7. find all comments that was authored by "ScumbagSteve"
  8. find all comments belonging to the post "Reports a bug in your code"
use execise
db.movies.insert([
{
title : "Fight Club",
writer : "Chuck Palahniuk",
year : 1999,
actors : [
"Brad Pitt",
"Edward Norton",
]
},
{
title : "Pulp Fiction",
writer : "Quentin Tarantino",
year : 1994,
actors : [
"John Travolta",
"Uma Thurman",
]
},
{
title : "Inglorious Basterds",
writer : "Quentin Tarantino",
year : 2009,
actors : [
"Brad Pitt",
"Diane Kruger",
"Eli Roth",
]
},
{
title : "The Hobbit: An Unexpected Journey",
writer : "J.R.R. Tolkein",
year : 2012,
franchise : "The Hobbit",
},
{
title : "The Hobbit: The Desolation of Smaug",
writer : "J.R.R. Tolkein",
year : 2013,
franchise : "The Hobbit",
},
{
title : "The Hobbit: The Battle of the Five Armies",
writer : "J.R.R. Tolkein",
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.",
},
{
title : "Pee Wee Herman's Big Adventure"
},
{
title : "Avatar"
}
])
db.users.insert([
{
username : "GoodGuyGreg",
first_name : "Good Guy",
last_name : "Greg"
},
{
username : "ScumbagSteve",
full_name : {
first : "Scumbag",
last : "Steve",
}
}
])
db.posts.insert([
{
_id: 1,
username : "GoodGuyGreg",
title : "Passes out at party",
body : "Wakes up early and cleans house",
},
{
_id: 2,
username : "GoodGuyGreg",
title : "Steals your identity",
body : "Raises your credit score",
},
{
_id: 3,
username : "GoodGuyGreg",
title : "Reports a bug in your code",
body : "Sends you a Pull Request",
},
{
_id: 4,
username : "ScumbagSteve",
title : "Borrows something",
body : "Sells it",
},
{
_id: 5,
username : "ScumbagSteve",
title : "Borrows everything",
body : "The end",
},
{
_id: 6,
username : "ScumbagSteve",
title : "Forks your repo on github",
body : "Sets to private",
},
])
db.comments.insert([
{
username : "GoodGuyGreg",
comment : "Hope you got a good deal!",
post : 4,
},
{
username : "GoodGuyGreg",
comment : "What's mine is yours!",
post : 5,
},
{
username : "GoodGuyGreg",
comment : "Don't violate the licensing agreement!",
post : 6,
},
{
username : "ScumbagSteve",
comment : "It still isn't clean",
post : 1,
},
{
username : "ScumbagSteve",
comment : "Denied your PR cause I found a hack",
post : 3
},
])
Q1. db.movies.find({}).pretty()
Q2. db.movies.find({writer: "Quentin Tarantino"}).pretty()
Q3. db.movies.find({"actors": "Brad Pitt"}).pretty()
Q4. db.movies.find({franchise: "The Hobbit"}).pretty()
Q5. db.movies.find({year: {$gte: 1990, $lte: 1999}}).pretty()
Q6. db.movies.find({$or:[{year:{$lte:2000}}, {year:{$gte:2010}}]}).pretty()
U1. db.movies.update({title: "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."}})
U2. db.movies.update({title: "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."}})
U3: db.movies.update({title: "Pulp Fiction"}, {$push: {actors: "Samuel L. Jackson"}})
TS1: db.movies.find({synopsis: /.Bilbo./}).pretty()
TS2: db.movies.find({synopsis: /.Gandalf./}).pretty()
TS3: db.movies.find({$and: [{synopsis: /.Bilbo./}, {synopsis: /.^Gandalf./}]}).pretty()
TS4: db.movies.find({$and: [{synopsis: /Bilbo/}, {synopsis: {$not: /Gandalf/}}]}).pretty()
TS5: db.movies.find({$or: [{synopsis: /dwarves/}, {synopsis: /hobbit/}]}).pretty()
D1: db.movies.remove({title: "Pee Wee Herman's Big Adventure"})
D2: db.movies.remove({title: "Avatar"})
R1: db.users.find({}).pretty()
R2: db.posts.find({}).pretty()
R3: db.posts.find({username: "GoodGuyGreg"}).pretty()
R4: db.posts.find({username: "ScumbagSteve"}).pretty()
R5: db.comments.find({}).pretty()
R6: db.comments.find({username: "GoodGuyGreg"}).pretty()
R7: db.comments.find({username: "ScumbagSteve"}).pretty()
R8: db.comments.find({post: db.posts.findOne({title: "Reports a bug in your code"})._id})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment