Skip to content

Instantly share code, notes, and snippets.

@tuxpower
Last active September 12, 2016 14:48
Show Gist options
  • Save tuxpower/c77cc5ab7ebe548c55f22b70e3950705 to your computer and use it in GitHub Desktop.
Save tuxpower/c77cc5ab7ebe548c55f22b70e3950705 to your computer and use it in GitHub Desktop.

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"

Solutions

> use mongo_practice
switched to db mongo_practice
>
>
> t = db.movies
mongo_practice.movies
>
>
> t
mongo_practice.movies
t.insert( { title : "Fight Club", writer : "Chuck Palahniuk", year : 1999, actors : [ "Brad Pitt" , "Edward Norton" ] } )
t.insert( { title : "Pulp Fiction", writer : "Quentin Tarantino", year : 1994, actors : [ "John Travolta", "Uma Thurman" ] } )
t.insert( { title : "Inglorious Basterds", writer : "Quentin Tarantino", year : 2009, actors : [ "Brad Pitt", "Diane Kruger", "Eli Roth" ] } )
t.insert( { title : "The Hobbit: An Unexpected Journey", writer : "J.R.R. Tolkein", year : 2012, franchise : "The Hobbit" } )
t.insert( { title : "The Hobbit: The Desolation of Smaug", writer : "J.R.R. Tolkein", year : 2013, franchise : "The Hobbit" } )
t.insert( { 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 e
ngage in a war against an array of combatants and keep the Lonely Mountain from falling into the hands of a rising darkness." } )
WriteResult({ "nInserted" : 1 })
t.insert( { title : "Pee Wee Herman's Big Adventure" } )
t.insert( { title : "Avatar" } )
t.find()
{ "_id" : ObjectId("57d2fb9f670debe4818af6e7"), "title" : "Fight Club", "writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] }
{ "_id" : ObjectId("57d2fc12670debe4818af6e8"), "title" : "Pulp Fiction", "writer" : "Quentin Tarantino", "year" : 1994, "actors" : [ "John Travolta", "Uma Thurman" ] }
{ "_id" : ObjectId("57d2fc63670debe4818af6e9"), "title" : "Inglorious Basterds", "writer" : "Quentin Tarantino", "year" : 2009, "actors" : [ "Brad Pitt", "Diane Kruger", "Eli Rot
h" ] }
{ "_id" : ObjectId("57d2fc94670debe4818af6ea"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcc4670debe4818af6eb"), "title" : "The Hobbit: The Desolation of Smaug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcfa670debe4818af6ec"), "title" : "The Hobbit: The Battle of the Five Armies", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "sy
nopsis" : "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." }
{ "_id" : ObjectId("57d2fd0c670debe4818af6ed"), "title" : "Pee Wee Herman's Big Adventure" }
{ "_id" : ObjectId("57d2fd1a670debe4818af6ee"), "title" : "Avatar" }
t.find( { writer : "Quentin Tarantino" } )
{ "_id" : ObjectId("57d2fc12670debe4818af6e8"), "title" : "Pulp Fiction", "writer" : "Quentin Tarantino", "year" : 1994, "actors" : [ "John Travolta", "Uma Thurman" ] }
{ "_id" : ObjectId("57d2fc63670debe4818af6e9"), "title" : "Inglorious Basterds", "writer" : "Quentin Tarantino", "year" : 2009, "actors" : [ "Brad Pitt", "Diane Kruger", "Eli Rot
h" ] }
t.find( { actors : "Brad Pitt" } )
{ "_id" : ObjectId("57d2fb9f670debe4818af6e7"), "title" : "Fight Club", "writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] }
{ "_id" : ObjectId("57d2fc63670debe4818af6e9"), "title" : "Inglorious Basterds", "writer" : "Quentin Tarantino", "year" : 2009, "actors" : [ "Brad Pitt", "Diane Kruger", "Eli Rot
h" ] }
t.find( { franchise : "The Hobbit" } )
{ "_id" : ObjectId("57d2fc94670debe4818af6ea"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcc4670debe4818af6eb"), "title" : "The Hobbit: The Desolation of Smaug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcfa670debe4818af6ec"), "title" : "The Hobbit: The Battle of the Five Armies", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "sy
nopsis" : "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." }
t.find( { year : { $lt : 2000 } } )
{ "_id" : ObjectId("57d2fb9f670debe4818af6e7"), "title" : "Fight Club", "writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] }
{ "_id" : ObjectId("57d2fc12670debe4818af6e8"), "title" : "Pulp Fiction", "writer" : "Quentin Tarantino", "year" : 1994, "actors" : [ "John Travolta", "Uma Thurman" ] }
t.find( { $or : [ { year : { $lt : 2000 } }, { year : { $gt : 2010 } } ] } )
{ "_id" : ObjectId("57d2fb9f670debe4818af6e7"), "title" : "Fight Club", "writer" : "Chuck Palahniuk", "year" : 1999, "actors" : [ "Brad Pitt", "Edward Norton" ] }
{ "_id" : ObjectId("57d2fc12670debe4818af6e8"), "title" : "Pulp Fiction", "writer" : "Quentin Tarantino", "year" : 1994, "actors" : [ "John Travolta", "Uma Thurman" ] }
{ "_id" : ObjectId("57d2fc94670debe4818af6ea"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcc4670debe4818af6eb"), "title" : "The Hobbit: The Desolation of Smaug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit" }
{ "_id" : ObjectId("57d2fcfa670debe4818af6ec"), "title" : "The Hobbit: The Battle of the Five Armies", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "sy
nopsis" : "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." }
t.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 dw
arves to reclaim their mountain home - and the gold within it - from the dragon Smaug." } } )
t.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." } } )
t.update( { title : "Pulp Fiction" }, { $push : { actors : "Samuel L. Jackson" } } )
t.find( { synopsis : /Bilbo/ } )
{ "_id" : ObjectId("57d2fcfa670debe4818af6ec"), "title" : "The Hobbit: The Battle of the Five Armies", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "sy
nopsis" : "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." }
{ "_id" : ObjectId("57d3013a670debe4818af6ef"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "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 S
maug." }
{ "_id" : ObjectId("57d2fcc4670debe4818af6eb"), "title" : "The Hobbit: The Desolation of Smaug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit", "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 mysteriou
s and magical ring." }
t.find( { synopsis : /Gandalf/ } )
{ "_id" : ObjectId("57d2fcc4670debe4818af6eb"), "title" : "The Hobbit: The Desolation of Smaug", "writer" : "J.R.R. Tolkein", "year" : 2013, "franchise" : "The Hobbit", "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 mysteriou
s and magical ring." }
t.find( { $and : [ { synopsis : /gold/ }, { synopsis : /dragon/ } ] } )
{ "_id" : ObjectId("57d3013a670debe4818af6ef"), "title" : "The Hobbit: An Unexpected Journey", "writer" : "J.R.R. Tolkein", "year" : 2012, "franchise" : "The Hobbit", "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 S
maug." }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment