Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

CS331 Homework 3 Sheet Due: Wednesday December 4th at 11:59 PM Objectives: In this homework you will try some simple operations with MongoDB. Skills 1. Learn how to use MongoDB 2. Write simple json...

1 answer below »
CS331 Homework 3 Sheet
Due: Wednesday December 4th at 11:59 PM
Objectives:
In this homework you will try some simple operations with MongoDB.
Skills
1. Learn how to use MongoDB
2. Write simple json file to structure the database and insert your data
3. Write simple queries to retrieve some data out of your database
Description:

The festival season is approaching! We all have some traditions to cele
ate, and part of these
traditions include food. You have been collecting recipes from all your family members to build
a Recipe book full of traditions for the family to use.
You decided to use MongoDB to build your recipes database.
The plan is to have a recipe scheme, with each recipe consisting of:
• A title
• Some ingredients
• An ingredient consists of a quantity, a unit of measurement, and a type.
• Example: 1-liter milk
• Some cooking instructions (a kind of algorithm)
• A category like "snack", "starter", "main course", "dessert", "
eakfast" etc.
Example: Please see example for one recipe at the end of the document
Homework Tasks:
1. Make a new collection named cookbook.
• Hint: if you insert or import documents into a collection that doesn't yet exist, the
collection will be automatically created.
2. Insert documents into the collection: Insert at least 5 documents for 5 different recips
into the collection (do not use the example provided as one of the 5 collection).
• You can use simple recipes like "hardboiled egg", "fried egg", "pancakes", "omelet", etc.
• Instructions should be very short.
• Some of the documents must include the following ingredients: milk, eggs and water.
3. Find documents in the collection using title and instructions
• Find all recipes with a title that starts with the letter "p".
• Find all recipes with a title that contains the letter "e".
• Find all recipes with a title that contains the letter "e" and an instruction that contains the
letter "a".
• Find all recipes with a title that contain the letter "e" or an instruction that contains the
letter "a".
4. Find documents in the collection using the ingredient a
ay
• Find all recipes with the ingredient "egg".
• Find all recipes without the ingredient "egg".
• Find all recipes with the ingredient "egg" and "sugar".
• Find all recipes with the ingredient "egg" or "sugar".
5. Counting the number of documents in a collection: General hint: The collection count
method
• Count how many recipes have exactly 3 ingredients
6. Aggregation
• For each category ("dessert", "
eakfast", etc.) find the number of dishes in the category.

What to Submit:

1. A JavaScript file named Homework3.js. The file should include the answers for all the
tasks above (2-6)
2. Your data file recipes.json
3. A document file with a screen shot for the execution of each task in the homework tasks
on Rob3T

Recipe Example
{
"title": "Tal’s Tahini “
ead pudding”",
"description": "A delicious baked dessert that takes 5 minutes to make!",
"category": [
"Dessert"
],
"ingredient": [
{
"amount": "0.5",
"unit": "cup",
"name": "tahini"
},
{
"amount": "4",
"name": "eggs"
},
{
"amount": "1",
"unit": "tsp",
"name": "baking powder"
http:
docs.mongodb.org/manual
eference/method/db.collection.count
http:
docs.mongodb.org/manual
eference/method/db.collection.count

},
{
"amount": "30",
"unit": "g",
"name": "honey"
},
{
"amount": "3",
"unit": "tbsp",
"name": "Sukrin Gold or
own sugar"
},
{
"amount": "1",
"unit": "tsp",
"name": "maple extract"
},
{
"name": "cinammon",
"amount": "1",
"unit": "tsp"
},
{
"name": "sesame seeds for garnish"
}
],
"step": [
{
"description": "Beat all ingredients together really well in a
lender."
},
{
"description": "Pour mixture in an oiled cake tray (we suggest sesame
oil for the oiling) and sprinkle with sesame seeds."
},
{
"description": "Bake for 25-30 minutes at 345F (convection bake is
etter). Depending on your oven, you may want to turn it around (horizontally)
after 15 minutes to ensure even baking."
},
{
"description": "Done, enjoy! We actually eat it with a spoon straight
from the cake tray!"
}
]
}

Dr. Sara Farag
CS331(Fall XXXXXXXXXXHomework Bonus
1
CS331Fall Bonus Homework
Due: Wednesday December 4th at 11:59 PM
Air quality

See video air pollution causes 7m premature deaths a year

After I watched the video above, I wanted to study the air pollution data and find out some
information’s out of it. You will be my little helper here.

I pulled some data from an Air quality database and the data set contains the information listed
elow. The attributes are for a simplified version of the Air Quality Index table (see description
elow).

location
Location
where
data was
measured
city
City
containing
location
country
Country
containing
measurement
in 2 letter ISO
code
pollutant
Name of
the
Pollutant
eing
measured.
Allowed
values:
PM25,
PM10,
SO2, NO2,
O3, CO, BC
value
Latest
measured
value for
the
pollutant
timestamp
The
datetime at
which the
pollutant
was
measured,
in ISO
8601
format
unit
The unit
the value
was
measured
in coded
y UCUM
Code
source
name
Name of
the
source of
the data
latitude
Latitude
in
decimal
degrees.
Precision
3
decimal
points.
longitude
Longitude
in
decimal
degrees.
Precision
3
decimal
points.
averaged_over_in_hours
The number of hours the
value was averaged over.

Here are the tasks that I would like you to complete so, I can continue working with this
dataset.
Tasks
1. Please use the data sample AQI.json file that I have pulled out from here and it is
available in the assignment box on canvas, then build a MongoDB database to store this
dataset.
Note: the dataset is very large, you need to patch them to smaller sets (500 max) and
insert each patch separately
2. Create an AQI collection and insert the data
3. Write a MongoDB queries to retrieve the quires below,
• List all unique Countries names in descending order that appeared in the Air
Quality Index (AQI) collection more than 10 times
• Retrieve the average value Latest measured with unit g/m of each city and then
eport the maximum
• The data listed are for some of the countries along with their source providers. For
example, AirNow provides Air Quality Reports for countries like Australia (AU),
United States (US), Chile (CL), Mexico (MX) and Canada (CA). Find out the unique
source names and their co
esponding reporting countries count. Sorted to show
the highest count first
• Retrieve Which countries use a unit other than ppm to measure any type of
pollution
https:
www.theguardian.com/environment/2016/may/12/air-pollution-rising-at-an-alarming-rate-in-worlds-cities
Dr. Sara Farag
CS331(Fall XXXXXXXXXXHomework Bonus
2
What to Submit:
• Please submit the java script file that includes all your queries
• A document file that includes screenshots for the execution of each query on Robo3T
    What to Submit:
     Please submit the java script file that includes all your queries
     A document file that includes screenshots for the execution of each query on Robo3T
Answered Same Day Nov 28, 2021

Solution

Kshitij answered on Nov 30 2021
136 Votes
q1_execution.rtf
Insert documents:
Tasks (2 to 6)
q1_Homework3.js
print("Find all recipes with a title that starts with letter 'p'");
db.cookbook.find({title: /.*e.*/ })
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with a title that contains the letter 'e'");
db.cookbook.find({title: /.*e.*/ })
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with a title that contains the letter 'e' and an instruction that contains the letter 'a'");
db.cookbook.find({title: /.*e.*/, "step.description": /.*a.*/})
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with a title that contain the letter 'e' or an instruction that contains the letter 'a'");
db.cookbook.find({ $or: [ { title: /.*e.*/ }, { "step.description": /.*a.*/ } ] })
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with the ingredient 'egg'");
db.cookbook.find({"ingredient.name": /.*egg.*/ })
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes without the ingredient 'egg'");
db.cookbook.find({ "ingredient.name": { $not: /.*egg.*/ }})
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with the ingredient 'egg' and 'sugar'");
db.cookbook.find({ "ingredient.name": /.*egg.*/, "ingredient.name": /.*sugar.*/ })
.forEach(function (rec) {
print(rec.title)
});
print("Find all recipes with the ingredient 'egg' or 'sugar'");
db.cookbook.find({ $or: [ { "ingredient.name": /.*egg.*/ }, { "ingredient.name": /.*sugar.*/ } ] })
.forEach(function (rec) {
print(rec.title)
});
print("documents in a collection");
db.cookbook.count();
print("Count how many recipes have exactly 3 ingredients");
let count = 0;
db.cookbook.aggregate([
    {
$project: {
item: 1,
name: "$title",
numIngredients: { $cond: { if: { $isA
ay: "$ingredient" }, then: { $size: "$ingredient" }, else: "NA"} }
        
}
     }
    ]).forEach(function (ing) {
if (ing.numIngredients == 3) {
count++;
            }

        });
print(count);
print("For each category...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here