MongoDB Lab Manual
MongoDB Lab Manual
LABORATORY MANUAL
mongoDB
(BAIL456B)
Common to
Department of Artificial Intelligence and Machine Learning
&
Department of Computer Science and Business Systems
Prepared By
Prof. Yatheesh N G, Assistant Professor, AI&ML dept.
Prof. PradeepKumar G M, Assistant Professor, CSBS Dept.
Course Outcomes:
Perform data analysis using aggregation operators and multi-stage aggregation pipelines.
CO3
Use indexing techniques like unique, sparse, compound, and multikey indexes to optimize
CO4 query performance.
Develop complex queries for real-world applications involving text search, filtering, and
CO5 dataset summarization.
Assessment Plan
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
Choice Based Credit System (CBCS)
SEMESTER – IV
MongoDB ([Link]) 1
(Effective from the academic year 2023-2024)
Course Code BAIL456B CIE Marks 50
Teaching Hours/Week (L:T:P: S) [Link] SEE Marks 50
Credits 01 Exam Hours 03
Examination type (SEE) Practical
Course Objectives:
This course aims to prepare the students to:
● Understand basic MongoDB functions, operators and types of operations in MongoDB.
● Demonstrate the use of Indexing, Advanced Indexing in MongoDB.
● Apply the aggregation and Map Reduction in MongoDB.
● Demonstrate text searching on collections in MongoDB.
[Link] Experiments
1 a. Illustration of Where Clause, AND,OR operations in MongoDB.
b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query,
Update, Delete and Projection. (Note: use any collection)
[Refer: Book 1 chapter 4].
2 a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents from any collection.
b. Develop a MongoDB query to display the first 5 documents from the results obtained in
a. [use of limit and find]
[Refer: Book1 Chapter 4, book 2: chapter 5]
3 a. Execute query selectors (comparison selectors, logical selectors ) and list out the results
on any collection
b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out the results
on any collection
[Refer: Book 3 Chapter 13]
4 Create and demonstrate how projection operators ($, $elematch and $slice) would be
used in the MongoDB.
[Refer: Book 3 Chapter 14]
5 Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.). students
encourage to execute several queries to demonstrate various aggregation operators)
[Refer: Book 3 Chapter 15]
6 Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group,
$sort, $project,$skip etc. students encourage to execute several queries to demonstrate
various aggregation operators)
[Refer book 2: chapter 6 ]
7 a. Find all listings with listing_url, name, address, host_picture_url in the listings And
Reviews collection that have a host with a picture url
b. Using E-commerce collection write a query to display reviews summary.
[refer Book2: chapter 6]
8 a. Demonstrate creation of different types of indexes on collection (unique, sparse,
compound and multikey indexes)
b. Demonstrate optimization of queries using indexes.
[Refer: Book 2: Chapter 8 and Book 3: Chapter 12]
9 a. Develop a query to demonstrate Text search using catalog data collection for a given word
b. Develop queries to illustrate excluding documents with certain words and phrases
[Refer: Book 2: Chapter 9]
10 Develop an aggregation pipeline to illustrate Text search on Catalog data collection.
[Refer: Book 2 :Chapter 9]
Course outcomes :
At the end of the course the student will be able to:
CO1: Make use of MangoDB commands and queries.
CO2: Illustrate the role of aggregate pipelines to extract data.
CO3 Demonstrate optimization of queries by creating indexes.
CO4: Develop aggregate pipelines for text search in collections.
Suggested Learning Resources:
BOOK 1: "MongoDB: The Definitive Guide", Kristina chodorow, 2nd ed O'REILLY, 2013.
BOOK 2: "MongoDB in Action" by KYLE BANKER et. al. 2nd ed, Manning publication, 2016
BOOK 3: "MongoDB Complete Guide" by Manu Sharma 1st ed, bpb publication, 2023.
instaIIation of MongoDB Video: [Link]
video on Aggregation: [Link]
MongoDB in action book Code downIoad URL: [Link]
MongoDB Exercise UR
Creating database
>use ProgrammingBooks
Or
> [Link]({ year:{$eq: 2008 }}).pretty( )
Or
> [Link]({$where:"[Link]==2008"}).pretty()
Syntax: $and
{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
Example: [Link]({
$and: [
{ category: "Software Development" },
{ year: 2008 }
]
}).pretty()
Syntax: $or
{ $or: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
Example:
[Link]({
$or: [
{ category: "JavaScript" },
{ year: 2015 }
]
}).pretty()
Examples:
>[Link]({ year: { $gt: 2000 } }).pretty( )
>[Link]({ year: { $gte: 2000 } }).pretty( )
>[Link]({ year: { $lt: 2000 } }).pretty( )
>[Link]({ year: { $lte: 2000 } }).pretty( )
>[Link]({ year: { $ne: 2000 } }).pretty( )
>[Link]({category:{$in:['javascript','Software Development']}}).pretty( )
>[Link]({category:{$nin:['javascript','Software Development']}}).pretty( )
Syntax: [Link](
{filter},
{ $set: { field1: value1 }, $inc: { filed2: value2 } },
...
)
Where:
Filter: The selection criteria for the update. The same query selectors as in the find()
method are available.
Specify an empty document { } to update the first document returned in the collection.
Example:
>[Link](
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "vtucode" } }
)
Note: updateMany( ) can also be used to add the field to existing documents
Example:
>[Link]({category:"Python"},
{$set:{title:"Python:Complete refrence"}},{returnDocumnet:"after"})
>[Link]({query:{category:'Python
Programming'},update:{$set:{title:"Python:For Begginers",year:2015}},new:true})
b. Develop a MongoDB query to display the first 5 documents from the results
obtained.[use of limit and find]
>[Link]( ) //displays number of documents in the collection
>[Link]().limit(5) //displays first 5 documents
>[Link]({},{category:1,title:1,price:1,_id:0}).limit(5)
comparison selectors:
$eq (Equal)
>[Link]({ department: { $eq: "Engineering" } }).pretty( )
logical selectors
$and Matches documents that match all conditions All must be true
$or Matches documents that match any condition At least one must be true
$not Inverts the result of a condition Opposite of the condition
$nor Matches documents that fail all conditions None should be true
Example: Find employees who are neither in the “HR” department nor have a salary greater
than 75000.
>[Link]({ $nor: [ { department: "HR" }, { salary: { $gt: 75000 } } ] }).pretty()
Geospatial selectors:
Geospatial selectors in MongoDB are used to store and query geospatial data like
coordinates, areas, and distances.
MongoDB supports 2D (flat plane) and 2dsphere (Earth-like) indexes, and has powerful
query operators for geospatial operations.
To specify GeoJSON data, use an embedded document with:
a field named type that specifies the GeoJSON object type, and
a field named coordinates that specifies the object's coordinates.
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }
Create collection
>use geodatabase
Note:
Geospatial indexes in MongoDB are designed to efficiently perform spatial queries, like finding
locations near a specific point, within a given radius, or within a particular shape.
They allow MongoDB to quickly filter and return documents based on their geographical
location without scanning the entire collection.
[Link]({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-73.9851, 40.7580]
},
$maxDistance: 5000 // distance in meters
}
}
}).pretty( )
Example: Find places within a specific polygon, for example, an area covering part of
Manhattan.
[Link]({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [
[
[-70.016, 35.715],
[-74.014, 40.717],
[-73.990, 40.730],
[-73.990, 40.715],
[-70.016, 35.715]
]
]
}
}
}
}).pretty()
Bitwise selectors
Bitwise selectors are query operators that allow you to perform bit-level queries on
integer fields.
These selectors are useful when you store multiple Boolean flags or states in a single
integer field using bit masking.
0 Is Active 0001 1
1 Is Manager 0010 2
2 Has Remote Access 0100 4
3 Is On Probation 1000 8
Example:
An employee with flags = 5 (binary 0101) means:
Is Active ✅
Is Manager ❌
Has Remote Access ✅
Is On Probation ❌
Create database
>use empDB
Create collection
>[Link](“employees”)
2. Get employees who are either managers OR have remote access (bit 1 or 2 set)
[Link]({ flags: { $bitsAnySet: [1, 2] } })
4. Get employees who are active and managers (bit 0 and 1 set)
[Link]({ flags: { $bitsAllSet: [0, 1] } })
4 Create and demonstrate how projection operators ($, $elematch and $slice)
would be used in the MongoDB.
Create database
>use retailDB
Create collection
>[Link](‘Products’)
[Link]([
{
name: "Laptop",
brand: "BrandA",
features: [
{ name: "Processor", value: "Intel i7" },
{ name: "RAM", value: "16GB" },
{ name: "Storage", value: "512GB SSD" }
],
reviews: [
{ user: "Alice", rating: 5, comment: "Excellent!" },
{ user: "Bob", rating: 4, comment: "Very good" },
{ user: "Charlie", rating: 3, comment: "Average" }
]
},
{
name: "Smartphone",
brand: "BrandB",
features: [
{ name: "Processor", value: "Snapdragon 888" },
{ name: "RAM", value: "8GB" },
{ name: "Storage", value: "256GB" }
],
reviews: [
{ user: "Dave", rating: 4, comment: "Good phone" },
{ user: "Eve", rating: 2, comment: "Not satisfied" }
]
}
])
Projection Operators:
1. The $ Projection Operator: used to project the first matching element from an array of
embedded documents.
Example: Find the product named “Laptop” and project the review from the user “Alice”.
> [Link](
{ name: "Laptop", "[Link]": "Alice" },
{ "reviews.$": 1 }
).pretty( )
2. The $elemMatch Projection Operator: used to project the first matching element from
an array based on specified criteria.
Example: Find the product named “Laptop” and project the review where the rating is greater
than 4.
> [Link](
{ name: "Laptop" },
{ reviews: { $elemMatch: { rating: { $gt: 4 } } } }
).pretty( )
Example: Find the product named “Smartphone” and project the first review.
>[Link](
{ name: "Smartphone" },
{ reviews: { $slice: 1 } }
).pretty( )
Example with Multiple Projection Operators: Find the product named “Laptop” and project
the name, the first two features, and the review with the highest rating.
Create database
> use salesDB
Create collection
>[Link](‘Sales’)
> [Link]([
{ date: new Date("2024-01-01"), product: "Laptop", price: 1200, quantity: 1, customer: "Amar" },
{ date: new Date("2024-01-02"), product: "Laptop", price: 1200, quantity: 2, customer: "Babu" },
{ date: new Date("2024-01-03"), product: "Mouse", price: 25, quantity: 5, customer: "Chandra" },
{ date: new Date("2024-01-04"), product: "Keyboard", price: 45, quantity: 3, customer: "Amar" },
{ date: new Date("2024-01-05"), product: "Monitor", price: 300, quantity: 1, customer: "Babu" },
{ date: new Date("2024-01-06"), product: "Laptop", price: 1200, quantity: 1, customer: "Deva" }
])
Aggregation Operations:
4. $push (Push Values to an Array): Group sales by customer and push each purchased product
into an array.
> [Link]([ { $group: { _id: "$customer",
products: { $push: "$product" } } } ]).pretty()
5. $addToSet (Add Unique Values to an Array): Group sales by customer and add each unique
purchased product to an array.
> [Link]([ { $group: { _id: "$customer",
uniqueProducts: { $addToSet: "$product" } } } ]).pretty()
Example: Calculate the total quantity and total sales amount for each product, and list all
customers who purchased each product.
[Link]([
{
$group: {
_id: "$product",
totalQuantity: { $sum: "$quantity" },
totalSales: { $sum: { $multiply: ["$price", "$quantity"] } },
customers: { $addToSet: "$customer" }
}
}
]).pretty( )
Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group,
6 $sort, $project,$skip etc. students encourage to execute several queries to demonstrate
various aggregation operators)
Create database
>use restaurantDB
Create collection
>[Link](‘restaurants’)
Insert documents
[Link]([
{
name: "Biryani House",
cuisine: "Indian",
location: "Jayanagar",
reviews: [
{ user: "Aarav", rating: 5, comment: "Amazing biryani!" },
{ user: "Bhavana", rating: 4, comment: "Great place!" }
]
},
{
name: "Burger Joint",
cuisine: "American",
location: "Koramangala",
reviews: [
{ user: "Chirag", rating: 3, comment: "Average burger" },
{ user: "Devika", rating: 4, comment: "Good value" }
]
},
{
name: "Pasta House",
cuisine: "Italian",
location: "Rajajinagar",
reviews: [
{ user: "Esha", rating: 5, comment: "Delicious pasta!" },
{ user: "Farhan", rating: 4, comment: "Nice ambiance" }
]
},
{
name: "Curry Palace",
cuisine: "Indian",
location: "Jayanagar",
reviews: [
{ user: "Gaurav", rating: 4, comment: "Spicy and tasty!" },
{ user: "Harini", rating: 5, comment: "Best curry in town!" }
]
},
{
name: "Taco Stand",
cuisine: "Mexican",
location: "Jayanagar",
reviews: [
{ user: "Ishaan", rating: 5, comment: "Fantastic tacos!" },
{ user: "Jaya", rating: 4, comment: "Very authentic" }
]
}
])
Aggregation Pipeline and its operations:
Execute Aggregation Pipeline and its operations (pipeline must contain match, group, sort,
project, $skip etc.)
[Link]([
{
$match: {
"location": "Jayanagar"
}
},
{
$unwind: "$reviews"
},
{
$group: {
_id: "$name",
averageRating: { $avg: "$[Link]" },
totalReviews: { $sum: 1 }
}
},
{
$sort: {
averageRating: -1
}
},
{
$project: {
_id: 0,
restaurant: "$_id",
averageRating: 1,
totalReviews: 1
}
},
{
$skip: 1
}
]).pretty( )
a. Find all listings with listing_url, name, address, host_picture_url in the listings And
7
Reviews collection that have a host with a picture url
Create database
> use vacationRentals
Create collection
>[Link](‘listingsAndReviews’)
Insert documents
[Link]([
{
listing_url: "[Link]
name: "Beautiful Apartment",
address: {
street: "123 Main Street",
suburb: "Central",
city: "Metropolis",
country: "Wonderland"
},
host: {
name: "Alice",
picture_url: "[Link]
}
},
{
listing_url: "[Link]
name: "Cozy Cottage",
address: {
street: "456 Another St",
suburb: "North",
city: "Smallville",
country: "Wonderland"
},
host: {
name: "Bob",
picture_url: ""
}
},
{
listing_url: "[Link]
name: "Modern Condo",
address: {
street: "789 Side Road",
suburb: "East",
city: "Gotham",
country: "Wonderland"
},
host: {
name: "Charlie",
picture_url: "[Link]
}
}
])
Create database
> use ecommerce
Create collection
>[Link](‘products’)
Insert documents
[Link]([
{
product_id: 1,
name: "Laptop",
category: "Electronics",
price: 1200,
reviews: [
{ user: "Alice", rating: 5, comment: "Excellent!" },
{ user: "Bob", rating: 4, comment: "Very good" },
{ user: "Charlie", rating: 3, comment: "Average" }
]
},
{
product_id: 2,
name: "Smartphone",
category: "Electronics",
price: 800,
reviews: [
{ user: "Dave", rating: 4, comment: "Good phone" },
{ user: "Eve", rating: 2, comment: "Not satisfied" },
{ user: "Frank", rating: 5, comment: "Amazing!" }
]
},
{
product_id: 3,
name: "Headphones",
category: "Accessories",
price: 150,
reviews: [
{ user: "Grace", rating: 5, comment: "Great sound" },
{ user: "Heidi", rating: 3, comment: "Okay" }
]
}
])
>[Link]([
{
$unwind: "$reviews"
},
{
$group: {
_id: "$name",
totalReviews: { $sum: 1 },
averageRating: { $avg: "$[Link]" },
comments: { $push: "$[Link]" }
}
},
{
$project: {
_id: 0,
product: "$_id",
totalReviews: 1,
averageRating: 1,
comments: 1
}
}
]).pretty( )
Explanation:
1. $unwind: Deconstructs the reviews array from each document to output a document for
each element.
2. $group: Groups the documents by product name, and calculates:
totalReviews: The total number of reviews for each product.
averageRating: The average rating of the reviews for each product.
comments: An array of all review comments for each product.
3. $project: Restructures the output documents to include the product name, total reviews,
average rating, and comments.
This only indexes documents where email exists, so it allows multiple documents
without an email field without violating the unique constraint (if unique: true is
combined).
Create database
> use kannadaMovies
Create collection
>[Link](‘catalog’)
Add documents through the csv file consisting of multiple documents(using Adddata button in
mongoDB compass )
The aggregation framework in MongoDB is a powerful tool for data processing and
transformation.
It consists of a series of stages, each stage performing an operation on the input documents
and passing the results to the next stage. This sequence of operations is called an
aggregation pipeline.
Step-by-Step guide to using the aggregation pipeline in MongoDB, with examples of
various stages such as $match, $group, $sort, $project, $skip, and others.
>[Link]({$project:{year:'$year',genre:1}})
Or
>[Link]({$project:{year:'$year',genre:’$genre’}})
result = [Link]([
{$match: {year :2017}}, // 1. Match stage: Filter documents by year 2017
{$group: {_id: "$genre", avgRating:{$avg: "$rating"}}}, // 2. Group stage: Group by genre and
calculate average rating
{$sort: {avgRating:-1}}, // 3. Sort stage: Sort by avgRating in descending order
{$project: {year:"$year", avgRating:1, genre:1} }, // 4. Project stage: Include specific fields
{$limit:5} ]).toArray() // 5. Limit stage: Limit the output to 5 results
print("Top 5 rated movie genres with their average rating")
printjson(result)
Example 2: Find the remaining five genres among the top-rated seven genres after skipping
the first two, we can use the $skip stage in the aggregation pipeline.
result2 = [Link]([
{$match: {year :2017}}, // 1. Match stage: Filter documents by year 2017
{$group: {_id: "$genre", avgRating:{$avg: "$rating"}}}, // 2. Group stage: Group by genre and
calculate average rating
{$sort: {avgRating:-1}}, // 3. Sort stage: Sort by avgRating in descending order
{$project: {year:"$year", avgRating:1, genre:1} }, // 4. Project stage: Include specific fields