Supervision 8 Possible Solutions
Supervision 8 Possible Solutions
Samuel Idowu
[email protected]
Jan, 2022
Useful Links
Jan, 2022
2
Question 1
The following MongoDB questions are based on Structure of ‘restaurants’ collection:
‘restaurants’ collection which contains 3772 documents.
Write a MongoDB query to: {
"address": {
a. Display all the documents in the collection
restaurants. "building": "1007",
b. Display the fields restaurant_id, name, borough "coord": [ -73.856077, 40.848447 ],
and cuisine for all the documents in the collection
"street": "Morris Park Ave",
restaurant.
c. Display the fields restaurant_id, name, borough "zipcode": "10462"
and zip code, but exclude the field _id for all the },
documents in the collection restaurant. "borough": "Bronx",
d. Display the first 5 restaurant which is in the
borough Bronx. "cuisine": "Bakery",
e. Find the restaurants that achieved a score, more "grades": [
than 80 but less than 100. { "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
f. find the restaurants which belong to the borough
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
Bronx and prepared either American or Chinese
dish { "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
g. Write a MongoDB query to find the restaurant Id, { "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
name, borough and cuisine for those restaurants
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
which achieved a score which is not more than
10. ],
h. Write a MongoDB query to arrange the name of "name": "Morris Park Bake Shop",
the restaurants in ascending order along with all
"restaurant_id": "30075445"
the columns.
}
Jan, 2022
3
> db.restaurants.find();
a. Display all the documents in the collection
restaurants.
a. Insert the following documents into a “movies” collection. year : 1999 writer : J.R.R. Tolkein
actors : [ year : 2012
b. Query the “movies” collection to Brad Pitt franchise : The Hobbit
○ Get all documents
Edward Norton
○ Get all documents with writer set to “Quentin Tarantino”
○ Get all documents where actors include “Brad Pitt” ]
○ Get all documents with franchise set to “The Hobbit” title : The Hobbit: The Desolation
○ Get all movies released in the 90s of Smaug
○ Get all movies released before the year 2000 or after 2010
title : Pulp Fiction writer : J.R.R. Tolkein
c. Update documents writer : Quentin Tarantino year : 2013
○ add a synopsis to "The Hobbit: An Unexpected Journey" : "A year : 1994 franchise : The Hobbit
reluctant hobbit, Bilbo Baggins, sets out to the Lonely Mountain
actors : [
with a spirited group of dwarves to reclaim their mountain
home - and the gold within it - from the dragon Smaug." John Travolta
○ add a synopsis to "The Hobbit: The Desolation of Smaug" : "The Uma Thurman title : The Hobbit: The Battle of the Five Armies
dwarves, along with Bilbo Baggins and Gandalf the Grey, ]
continue their quest to reclaim Erebor, their homeland, from writer : J.R.R. Tolkein
Smaug. Bilbo Baggins is in possession of a mysterious and year : 2012
magical ring." franchise : The Hobbit
○ add an actor named "Samuel L. Jackson" to the movie "Pulp
synopsis : Bilbo and Company are forced to
Fiction" title : Inglorious Basterds
engage in a war against an array of
writer : Quentin Tarantino
d. Text Search combatants and keep the Lonely Mountain
year : 2009
○ find all movies that have a synopsis that contains the word from falling into the hands of a rising
"Bilbo" actors : [
darkness.
○ find all movies that have a synopsis that contains the word Brad Pitt
"Gandalf" Diane Kruger
○ find all movies that have a synopsis that contains the word
Eli Roth
"Bilbo" and not the word "Gandalf"
○ find all movies that have a synopsis that contains the word ]
"dwarves" or "hobbit"
e. Delete documents
○ delete the movie "Pulp Fiction"
○ delete the movie "The Hobbit: The Desolation"
Jan, 2022
8
// create database
use supervision_8
//create collection
db.createCollection("movies")
Jan, 2022
9
// insertmany documents
db.movies.insertMany([
{
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'
]
},
Jan, 2022])
10
/ Query the movies collection
// Get all documents
db.movies.find().pretty()
// Get all movies released before the year 2000 or after 2010
db.movies.find({ $or: [{ year: { $lt: 2000 } }, { year: { $gt: 2010 } }] }).pretty()
Jan, 2022
11
// Update documents
// 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({ 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.
" } })
// 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({ 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.
" } })
Jan, 2022
12
//Text Search
// find all movies that have a synopsis that contains the word "Bilbo"
db.movies.find({ synopsis: /Bilbo/g }).pretty()
// find all movies that have a synopsis that contains the word "Gandalf"
db.movies.find({ synopsis: /Gandalf/g }).pretty()
// find all movies that have a synopsis that contains the word "Bilbo" and not the word "Gandalf"
db.movies.find({ $and: [{ synopsis: /Bilbo/g }, { synopsis: { $not: /Gandalf/g } }] }).pretty()
// find all movies that have a synopsis that contains the word "dwarves" or "hobbit"
db.movies.find({ synopsis: /(dwarves|hobbit)/g }).pretty()
db.movies.find({ $or: [{ synopsis: /dwarves/g }, { synopsis: /hobbit/g }] }).pretty()
// find all movies that have a synopsis that contains the word "gold" and "dragon"
db.movies.find({ synopsis: /(gold.*dragon|dragon.*gold)/g }).pretty()
Jan, 2022
13
//Delete Documents
Jan, 2022
14
Questions? Jan, 2022
15