0% found this document useful (0 votes)
12 views

BC2402 Week 10 Class Exercises

This document provides instructions for completing exercises using MongoDB. It includes instructions to import data into collections, perform queries to find specific documents, and update documents. For the first case, students are asked to import a JSON file into a MongoDB collection and write queries to find movies by genre and year. For the second case, they are instructed to insert and update documents in a sports teams collection. For the third case, students must write a MongoDB pipeline to find the number of people over 50 grouped by gender, with the average age and total count for each group sorted in descending order.

Uploaded by

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

BC2402 Week 10 Class Exercises

This document provides instructions for completing exercises using MongoDB. It includes instructions to import data into collections, perform queries to find specific documents, and update documents. For the first case, students are asked to import a JSON file into a MongoDB collection and write queries to find movies by genre and year. For the second case, they are instructed to insert and update documents in a sports teams collection. For the third case, students must write a MongoDB pipeline to find the number of people over 50 grouped by gender, with the average age and total count for each group sorted in descending order.

Uploaded by

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

BC2402: Designing and Developing Databases

Module BC2402 Weekly Exercises - Week 10

Prof. Chan Wai Xin

Sem: 9

Group: 7

Names: Hou Chen Ying @ Hou Jenny


Nur Hafizah Binte Azeni
Teo Su Ning Petrissia
Than Lwin Mue Thaw (Gina)
Woon Jun Wei

Case: Box Office 2

Download and import boxoffice2.json onto your MongoDB environment.

Database name: boxoffice2, Collection Name: boxofficeExtended

Import boxoffice2.json into MongoDB

Required:

1. Find all movies with exactly two genres.


db.boxofficeExtended.find({genre: {$size: 2}})
2. Find all movies which aired in 2018.
db.boxofficeExtended.find({"meta.aired": 2018})
3. Find all movies which have ratings greater than 8 but lower than 10.
db.boxofficeExtended.find({$and: [{"meta.rating": {$gt: 8}},{"meta.rating":{$lt: 10}}]})

Case: Sports Teams

Database name: sports

Collection Name: teams

Required:
1. Insert the following documents using upsert, not insert( ).

{
"title" : "Nanyang United",
"requiresTeam" : true
},
{
"title" : "Sengkang One",
"requiresTeam" : false
}

use sports

db.teams.updateMany(

{"title" : "Nanyang United"},

{$set: {"requiresTeam" : true}}

,{upsert:true})

db.teams.updateMany(

{"title" : "Sengkang One"},

{$set: {"requiresTeam" : false}}

,{upsert:true})

db.teams.find()

2. Update all documents which do require a team by adding a new field with the minimum amount
of players required.

db.teams.updateMany(
{"requiresTeam" : true},
{$set: {"MinNoPlayers" : 0}}
,{upsert:true})

3. Update all documents that require a team by increasing the number of required players by 10.

db.teams.updateMany(

{"requiresTeam" : true},

{$inc: {"MinNoPlayers" : 10}})


Case: Persons

Continue with the person.json that is used in the slide deck.

Required:

Build a pipeline to find the number of person(s) older than 50. Group these people by gender. For each
group, compute the average age and count the total number of person(s) in the group. Order the
output by the total amount of person(s) per group in a descending order.

$avg operator https://docs.mongodb.com/manual/reference/operator/aggregation/avg/

use "analytics"
db.persons.find()
db.persons.aggregate([
{$match: {"dob.age":{$gt: 50}}},
{$group: { _id: "$gender", avgAge: {$avg: "$dob.age"}, totalCount: {$sum:1}}},
{$sort: {totalCount: -1}}
])

You might also like