0% found this document useful (0 votes)
7 views18 pages

DataBase Lab Assignment 4 Updated

This document is a database lab assignment submitted by Syeda Eman Fatima, detailing tasks related to MongoDB and data aggregation. It includes questions on importing movie data, analyzing genres, and performing various aggregation operations. Additionally, it covers practical tasks such as calculating total sales, sorting students by marks, and grouping books by authors.

Uploaded by

fatimasyed4700
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)
7 views18 pages

DataBase Lab Assignment 4 Updated

This document is a database lab assignment submitted by Syeda Eman Fatima, detailing tasks related to MongoDB and data aggregation. It includes questions on importing movie data, analyzing genres, and performing various aggregation operations. Additionally, it covers practical tasks such as calculating total sales, sorting students by marks, and grouping books by authors.

Uploaded by

fatimasyed4700
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

DATABASE LAB ASSIGNMENT 4

DATABASE
LAB ASSIGNMENT 4

Date: 5/21/2026

Submitted To: Mam Sadia Maryam

Submitted By: Syeda Eman Fatima

Reg Number: SP25-BCT-046


DATABASE LAB ASSIGNMENT 4

Contents
Question 1: ................................................................................................. 2
Part 1: .................................................................................................... 2
Part 2: .................................................................................................... 3
Part 3: .................................................................................................... 4
Part 4: .................................................................................................... 5
Part 5: .................................................................................................... 6
Part 6: .................................................................................................... 7
Part 7: .................................................................................................... 8
Relational Operators ....................................................................... 8
Array Operators ................................................................................ 9
Logical Operators ............................................................................. 9
Question 2: ............................................................................................... 10
Section A: Short Question Answers ................................................. 10
Part 1: .............................................................................................. 10
Part 2: .............................................................................................. 11
Part 3: .............................................................................................. 11
Part 4: .............................................................................................. 12
Part 5: .............................................................................................. 13
Section B: Practical Tasks ................................................................. 13
Part 1: .............................................................................................. 13
Part 2: .............................................................................................. 14
Part 3: .............................................................................................. 15
Part 4: .............................................................................................. 16
Part 5: .............................................................................................. 16

P a g e 1 | 17
DATABASE LAB ASSIGNMENT 4

Question 1:
Download the movies collection in json format from
[Link]
FfC/view?usp=sharing and import it to MongoDB.

Part 1:
How many documents were imported?

23539 documents were imported in total


P a g e 2 | 17
DATABASE LAB ASSIGNMENT 4

Part 2:
Using MongoDB aggregation framework over the movies collection, find
out the number of movies of each genre. How many movies fall under
“Thriller” genre?

There are a total of 2658 movies with thriller genres


P a g e 3 | 17
DATABASE LAB ASSIGNMENT 4

Part 3:
How many movies in the collection have IMDB rating greater than or
equal to 9.5?

There are a total of 2 movies with IMDB rating higher than 9.5 Part

P a g e 4 | 17
DATABASE LAB ASSIGNMENT 4

Part 4:
Which movie has won the most awards?

P a g e 5 | 17
DATABASE LAB ASSIGNMENT 4

Part 5:
How many movies are there in the collection that belong to Comedy
genre, have IMDB rating greater than 8.0, and have won more than 50
awards?

There are a total of 8 movies with comedic genre, have won more than
50 awards and have an IMDB rating greater than 8.0

P a g e 6 | 17
DATABASE LAB ASSIGNMENT 4

Part 6:
Create a query using $match, $group, $count, $sort and $project by using
Mongo-Shell. In addition, provide stages of pipeline being followed in
mongo-db.

P a g e 7 | 17
DATABASE LAB ASSIGNMENT 4

Part 7:
Create queries using all concepts regarding relational operators , array
operations , logical operators.

Relational Operators

P a g e 8 | 17
DATABASE LAB ASSIGNMENT 4

Array Operators

Logical Operators

P a g e 9 | 17
DATABASE LAB ASSIGNMENT 4

Question 2:
MONGODB AGGREGATION & PIPELINING

Section A: Short Question Answers


Part 1:
What is an aggregation pipeline in MongoDB? Explain its purpose.

Answer:

An aggregation pipeline in MongoDB is a data processing framework


that transforms and analyzes documents through a sequence of
operations. Each stage in the pipeline performs a specific operation on
the incoming data and forwards its output to the following stage.

Purpose of the Aggregation Pipeline:

• Filter and retrieve specific documents based on conditions

• Group related documents together

• Perform calculations such as sum, average, min, and max

• Sort query results in a desired order

• Restructure or reshape document fields

• Carry out complex data analysis and reporting tasks

It operates similarly to an assembly line — data enters at one end,


undergoes a series of transformations at each stage, and the final
processed result exits at the other end.

P a g e 10 | 17
DATABASE LAB ASSIGNMENT 4

Part 2:
Differentiate between $match and $project stages.

Feature $match $project

Purpose Filters documents based Selects, hides, or reshapes


on conditions document fields

Equivalent in WHERE clause SELECT clause


SQL

Operates on Entire documents Individual fields within


documents

Output Only documents that Documents with a


satisfy the condition modified field structure

Part 3:
What is the use of the $group stage? Provide one example.

Answer

The $group stage is used to consolidate multiple documents that share a


common field value into a single grouped result. It supports a variety of
accumulator operations including $sum, $avg, $count, $max, and $min.

Example:

[Link]([

$group: {

P a g e 11 | 17
DATABASE LAB ASSIGNMENT 4

_id: "$category",

totalSales: { $sum: "$amount" }

])

This query groups all sales records by their category and computes the
combined sales amount for each one.

Part 4:
Why is the aggregation pipeline more powerful than simple find()
queries?

Answer:

The aggregation pipeline offers far greater capabilities compared to a


basic find() query. It can execute mathematical and statistical
calculations, consolidate documents into groups, modify or reshape
document structures, combine data from multiple collections using
$lookup, apply sorting, and chain multiple processing stages together for
complex workflows.

In contrast, find() is mainly limited to fetching documents from a


collection and applying basic filters and field projections.

In summary, the aggregation pipeline is the preferred tool for data


analysis, reporting, and any task that requires more than simple
document retrieval.

P a g e 12 | 17
DATABASE LAB ASSIGNMENT 4

Part 5:
What is the difference between $sort and $sortByCount?

Feature $sort $sortByCount

Purpose Orders documents by Groups documents and orders


specified fields by occurrence count

Output Same documents in a Grouped results with a count


new order for each value

Requires No Yes (done automatically)


Grouping?

Usage Used when you just Used when you need to count
need to sort and sort together

Section B: Practical Tasks


Part 1:
Total sales per product — SALES(ProductID, Quantity, Price)

Answer

[Link]([

$group: {

_id: "$ProductID",

totalSales: {

$sum: { $multiply: ["$Quantity", "$Price"] }


P a g e 13 | 17
DATABASE LAB ASSIGNMENT 4

])

Explanation Documents are grouped based on the ProductID field. For


each document, Quantity is multiplied by Price to calculate revenue. The
$sum accumulator then adds up all these values to produce total sales
per product.

Part 2:
Top 3 students with highest marks — STUDENTS(Name, Marks)

Answer

[Link]([

{ $sort: { Marks: -1 } },

{ $limit: 3 }

])

Explanation $sort arranges all student records from highest marks to


lowest (-1 means descending order). $limit then restricts the output to
only the first 3 results, which are the top 3 students.

P a g e 14 | 17
DATABASE LAB ASSIGNMENT 4

Part 3:
Count books per author — BOOKS(Title, Author, Year)

Answer

[Link]([

$match: { Year: { $gte: 2000 } }

},

$group: {

_id: "$Author",

totalBooks: { $sum: 1 }

])

Explanation $match filters the collection to include only books


published in the year 2000 or later. $group then organizes the remaining
documents by author name. $sum: 1 increments the count by 1 for each
book belonging to that author.

P a g e 15 | 17
DATABASE LAB ASSIGNMENT 4

Part 4:
Sort employees by salary — EMP(ename, dept, salary)

Answer

[Link]([

$sort: { salary: -1 }

])

Explanation Using -1 as the sort value arranges all employees in


descending order by salary. The employee with the highest salary will
appear at the top of the result list.

Part 5:
Show Name and Age (renamed to Years) — PERSON collection

Answer

[Link]([

$project: {

_id: 0,

Name: 1,

Years: "$Age"
P a g e 16 | 17
DATABASE LAB ASSIGNMENT 4

])

Explanation Name: 1 includes the Name field in the output. Years:


"$Age" renames the Age field to Years in the result. _id: 0 suppresses the
default _id field so it does not appear in the output.

P a g e 17 | 17

You might also like