0% found this document useful (0 votes)
38 views15 pages

Supervision 8 Possible Solutions

Uploaded by

gushaemy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

Supervision 8 Possible Solutions

Uploaded by

gushaemy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Supervision 8 - NoSQL

DIT033: Data Management

Samuel Idowu
[email protected]
Jan, 2022
Useful Links

MongoDb Query/projection operators:


https://docs.mongodb.com/manual/reference/operator/query/

MongoDB CRUD operations:


https://docs.mongodb.com/manual/crud/

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.

b. Display the fields restaurant_id, name, borough > db.restaurants.find({},{"restaurant_id" :


and cuisine for all the documents in the collection 1,"name":1,"borough":1,"cuisine" :1});
restaurant.

c. Display the fields restaurant_id, name, borough


and zip code, but exclude the field _id for all the > db.restaurants.find({},{"restaurant_id" :
documents in the collection restaurant. 1,"name":1,"borough":1,"address.zipcode" :1,"_id":0});
d. Display the first 5 restaurant which is in the
borough Bronx.
> db.restaurants.find({"borough": "Bronx"}).limit(5);
e. Find the restaurants that achieved a score, more
than 80 but less than 100.

> db.restaurants.find({grades : { $elemMatch:{"score":{$gt :


80 , $lt :100}}}});

Possible Solution Jan, 2022


4
db.restaurants.find(
{ "borough": "Bronx",

f. find the restaurants which belong to the borough $or : [


Bronx and prepared either American or Chinese { "cuisine": "American " },
dish
{ "cuisine": "Chinese" }
]
}
);

Possible Solution Jan, 2022


5
db.restaurants.find(
g. Write a MongoDB query to find the restaurant Id, name, {
borough and cuisine for those restaurants which achieved a
score which is not more than 10. "grades.score": { $not: {$gt : 10
}
}
},
{
"restaurant_id": 1,
"name": 1,
"borough": 1,
"cuisine": 1
}
);

Possible Solution Jan, 2022


6
h. Write a MongoDB query to arrange the name of the
restaurants in ascending order along with all the columns.
db.restaurants.find().sort({"name":1});

Possible Solution Jan, 2022


7
Question 2 title : Fight Club title : The Hobbit: An Unexpected
Create a new mongo database named “supervision_8”.
writer : Chuck Palahniuk Journey

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")

//insert documents insertOne/insertMany


db.movies.insertOne(
{
title: 'Fight Club',
writer: 'Chuck Palahniuk',
year: 1999,
actors: [
'Brad Pitt',
'Edward Norton'
]
})

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 documents with writer set to "Quentin Tarantino"


db.movies.find({ writer: 'Quentin Tarantino' }).pretty()

// Get all documents where actors include "Brad Pitt"


db.movies.find({ actors: 'Brad Pitt' }).pretty()

// Get all documents with franchise set to "The Hobbit"


db.movies.find({ franchise: 'The Hobbit' }).pretty()

// Get all movies released in the 90s


db.movies.find({ year: { $gte: 1990, $lte: 1999 } }).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.
" } })

// add an actor named "Samuel L. Jackson" to the movie "Pulp Fiction"


db.movies.update({ title: 'Pulp Fiction' }, { $push: { actors: 'Samuel L. Jackson' } })

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

// delete the movie "The Hobbit: The Desolation of Smaug"


db.movies.deleteMany({ title: "The Hobbit: The Desolation of Smaug
" })

Jan, 2022
14
Questions? Jan, 2022
15

You might also like