Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

Belagavi, Karnataka-590 014

Laboratory Manual
on
MONGODB (BDSL456B)

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING

ACHARYA INSTITUTE OF TECHNOLOGY

BANGALORE

2023-2024

COMPILED BY

Mr. Jovin Deglus


Vision & Mission of the Institute

Vision of the Institute

Acharya Institute of Technology, committed to the cause of value-based education in all


disciplines, envisions itself as a fountainhead of innovative human enterprise, with
inspirational initiatives for Academic Excellence.

Mission of the institute

Acharya Institute of Technology strives to provide excellent academic ambiance to the


students for achieving global standards of technical education, foster intellectual and
personal development, meaningful research and ethical service to sustainable societal
needs.

Motto of the institute

“Nurturing Aspirations Supporting Growth”


Vision & Mission of the Department

Vision of the Department

To establish as a centre of excellence in molding young professionals in the domain of


Artificial Intelligence and Machine Learning who are capable of contributing to the
wellness of the society as well as the industry.

Mission of the Department

1. To nurture the students with multi-disciplinary skills to be able to build


sustainable solutions to engineering problems which in turn helps them to grab
dynamic and promising career in the industry.
2. To mould energetic and committed engineers by inculcating moral values of
professional ethics, social concerns, environment protection, sustainable
solutions and life-long learning.
3. To facilitate research cooperation in the fields of artificial intelligence, machine
learning, and data analytics by handholding with national and international
research organizations, thus enabling the students to analyse and apply various
levels of learning.
Program Outcomes (POs)

Engineering Graduates will be able to:

1. Engineering knowledge: Apply the knowledge of mathematics, science,


engineering fundamentals, and an engineering specialization to the solution of
complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first
principles of mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified
needs with appropriate consideration for the public health and safety, and the
cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of
data, and synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modelling to
complex engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge
to assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate
the knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and
responsibilities and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member
or leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with
the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make
effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of
the engineering and management principles and apply these to one’s own work,
as a member and leader in a team, to manage projects and in multidisciplinary
environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of
technological change.
Program Educational Objectives (PEOs)

Engineering Graduates will

1. Have a strong foundation in key concepts and techniques related to artificial


intelligence and its related fields, thus enabling them to analyze complex problems
in various domains and apply AI and ML techniques to develop innovative and
effective solutions.
2. Be conscious of the ethical implications of AI/ML technologies, be aware of
potential biases and ensure fairness, transparency, and accountability and
demonstrate a commitment to develop AI/ML applications for the betterment of
society.
3. Possess effective communication skills and be capable of working collaboratively
in multidisciplinary teams to address real-world challenges.
4. Have an entrepreneurial mindset and be equipped with the skills and knowledge
to create innovative AI/ML solutions and potentially start their own ventures.
5. Demonstrate a commitment to lifelong learning and stay updated with
advancements in AI and ML technologies and industry trends to emerge as leaders
in their fields of expertise and domain that support service and economic
development of the society.

Program Specific Outcomes (PSOs)

Engineering Graduates will be able to:

1. Apply the Artificial Intelligence and Data Analytics skills in the areas of Disaster
Management, Health Care, Education, Agriculture, Transportation, Environment,
Society and in other multi-disciplinary areas.
2. Analyze and demonstrate the knowledge of Human cognition, Artificial
Intelligence, Machine Learning and Data engineering in terms of real-world
problems to meet the challenges of the future.
3. Develop computational knowledge and project management skills using
innovative tools and techniques to solve problems in the areas related to Deep
Learning, Machine learning, Artificial Intelligence which will lead to lifelong
learning.
MongoDB
Course Code BDSL456B CIE Marks 50
Teaching Hours/Week (L:T:P: S) 0:1:2:0 SEE Marks 50
Total Hours of Pedagogy 24 Total Marks 100
Credits 01 Exam Hours 03
Course Learning Objectives:

● 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.
Teaching-Learning Process (General Instructions)
These are sample Strategies, which teacher can use to accelerate the attainment of the various course outcomes.

1. Lecturer method (L) does not mean only traditional lecture method, but different type of
teaching methods may be adopted to develop the outcomes.
2. Show Video/animation films to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Ask at least three HOTS (Higher order Thinking) questions in the class, which promotes critical
thinking.
5. Adopt Problem Based Learning (PBL), which fosters students’ Analytical skills, develop thinking
skills such as the ability to evaluate, generalize, and analyze information rather than simply recall it.
6. Topics will be introduced in a multiple representation.
7. Show the different ways to solve the same problem and encourage the students to come up with their
own creative ways to solve them.
8. Discuss how every concept can be applied to the real world - and when that's possible, it helps
improve the students' understanding.
SL # 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]
[Refe: 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
MondoDB.
[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 (Course Skill Set):

At the end of the course the student will be able to:


1. Make use of MongoDB commands and queries.
2. Illustrate the role of aggregate pipelines to extract data and for text search in collections.
3. Demonstrate optimization of queries by creating indexes.

Assessment Details (both CIE and SEE)


The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%.
The minimum passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and for the
SEE minimum passing mark is 35% of the maximum marks (18 out of 50 marks). A student shall be
deemed to have satisfied the academic requirements and earned the credits allotted to each subject/
course if the student secures a minimum of 40% (40 marks out of 100) in the sum total of the CIE
(Continuous Internal Evaluation) and SEE (Semester End Examination) taken together
Continuous Internal Evaluation (CIE):
CIE marks for the practical course are 50 Marks.
The split-up of CIE marks for record/ journal and test are in the ratio 60:40.
• Each experiment is to be evaluated for conduction with an observation sheet and record write-up. Rubrics
for the evaluation of the journal/write-up for hardware/software experiments are designed by the faculty
who is handling the laboratory session and are made known to students at the beginning of the practical
session.
• Record should contain all the specified experiments in the syllabus and each experiment write-up will be
evaluated for 10 marks.
• Total marks scored by the students are scaled down to 30 marks (60% of maximum marks).
• Weightage to be given for neatness and submission of record/write-up on time.
• Department shall conduct a test of 100 marks after the completion of all the experiments listed in the
syllabus.
• In a test, test write-up, conduction of experiment, acceptable result, and procedural knowledge will carry
a weightage of 60% and the rest 40% for viva-voce.
• The suitable rubrics can be designed to evaluate each student’s performance and learning ability.
• The marks scored shall be scaled down to 20 marks (40% of the maximum marks).
• The Sum of scaled-down marks scored in the report write-up/journal and marks of a test is the total CIE
• marks scored by the student.

Semester End Evaluation (SEE):


• SEE marks for the practical course are 50 Marks.
• SEE shall be conducted jointly by the two examiners of the same institute; examiners are appointed
by the Head of the Institute.
• The examination schedule and names of examiners are informed to the university before the
conduction of the examination. These practical examinations are to be conducted between the
schedule mentioned in the academic calendar of the University.
• All laboratory experiments are to be included for practical examination.
• (Rubrics) Breakup of marks and the instructions printed on the cover page of the answer script to be
strictly adhered to by the examiners. OR based on the course requirement evaluation rubrics shall be
decided jointly by examiners.
• Students can pick one question (experiment) from the questions lot prepared by the examiners
jointly.
• Evaluation of test write-up/ conduction procedure and result/viva will be conducted jointly by
examiners.
• General rubrics suggested for SEE are mentioned here, writeup-20%, Conduction procedure and
result in -60%, Viva-voce 20% of maximum marks. SEE for practical shall be evaluated for 100
marks and scored marks shall be scaled down to 50 marks (however, based on course type, rubrics
shall be decided by the examiners)
• Change of experiment is allowed only once and 15% of Marks allotted to the procedure part are to
bemade zero.
• The minimum duration of SEE is 02 hours
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.
• installation of MongoDB Video: https://www.youtube.com/watch?v=dEm2AS5amyA
• video on Aggregation: https://www.youtube.com/watch?v=vx1C8EyTa7Y
• MongoDB in action book Code download URL: https://www.manning.com/downloads/529
• MongoDB Exercise URL: https://www.w3resource.com/mongodb-exercises/
Introduction to NoSQL Databases
A database Management System provides the mechanism to store and retrieve the data.
There are different kinds of database Management Systems:

1. RDBMS (Relational Database Management Systems)


2. OLAP (Online Analytical Processing)
3. NoSQL (Not only SQL)

NoSQL databases were created to overcome the limitations of relational databases.

What is a NoSQL database?

NoSQL databases are different than relational databases like MS Sql. In relational
database you need to create the table, define schema, set the data types of fields etc before
you can actually insert the data. In NoSQL you don’t have to worry about that, you can
insert, update data on the fly.

One of the advantages of NoSQL database is that they are really easy to scale and they are
much faster in most types of operations that we perform on database. There are certain
situations where you would prefer relational database over NoSQL, however when you
are dealing with huge amount of data then NoSQL database is your best choice.

Limitations of Relational databases

1. In relational database we need to define structure and schema of data first and
then only we can process the data.
2. Relational database systems provide consistency and integrity of data by
enforcing ACID properties (Atomicity, Consistency, Isolation and Durability).
There are some scenarios where this is useful like banking system. However in
most of the other cases these properties are significant performance overhead and
can make your database response very slow.
3. Most of the applications store their data in JSON format and RDBMS don’t provide
you a better way of performing operations such as create, insert, update, delete
etc on this data. On the other hand NoSQL store their data in JSON format, which
is compatible with most of the today’s world application.

What are the advantages of NoSQL

There are several advantages of working with NoSQL databases such as MongoDB and
Cassandra. The main advantages are high scalability and high availability.

High scalability: NoSQL database such as MongoDB uses sharding for horizontal scaling.
Sharding is partitioning of data and placing it on multiple machines in such a way that the
order of the data is preserved. Vertical scaling means adding more resources to the
existing machine while horizontal scaling means adding more machines to handle the
data. Vertical scaling is not that easy to implement, on the other hand horizontal scaling
is easy to implement. Horizontal scaling database examples: MongoDB, Cassandra etc.
Because of this feature NoSQL can handle huge amount of data, as the data grows NoSQL
scale itself to handle that data in efficient manner.

High Availability: Auto replication feature in MongoDB makes it highly available


because in case of any failure data replicates itself to the previous consistent state.

Types of NoSQL database

Here are the types of NoSQL databases and the name of the databases system that falls in
that category. MongoDB falls in the category of NoSQL document based database.

Key Value Store: Memcached, Redis, Coherence

Tabular: Hbase, Big Table, Accumulo

Document based: MongoDB, CouchDB, Cloudant

RDBMS Vs NoSQL
RDBMS: It is a structured data that provides more functionality but gives less
performance.

NoSQL: Structured or semi structured data, less functionality and high performance.

So when I say less functionality in NoSQL what’s missing:

1. You can’t have constraints in NoSQL

2. Joins are not supported in NoSQL

These supports actually hinders the scalability of a database, so while using NoSQL
database like MongoDB, you can implement these functionalities at the application level.
When to go for NoSQL

1. When you would want to choose NoSQL over relational database:


2. When you want to store and retrieve huge amount of data.
3. The relationship between the data you store is not that important
4. The data is not structured and changing over time
5. Constraints and Joins support is not required at database level
6. The data is growing continuously and you need to scale the database regular to
handle the data.

What is MongoDB ?

• MongoDB is a opensource, cross-platform, document-oriented database program.


• Classified as a NoSQL Database program, MongoDB uses JSON-Like documents.
• MongoDB is a learning NoSQL database.

Differences between MongoDB & SQL:

SQL Database NoSQL Database (MongoDB)

Is Relational database Is non-relational database

It supports SQL query language It supports JSON query language

It is mostly Collection-based and key-value pair


It is generally Table-based structure structure

It follows Row-based structure It follows Document-based structure

It follows Column-based structure It follows Field-based structure

It supports foreign keys It does not support foreign keys

It supports triggers It does not support triggers

Schema is predefined Schema is dynamic

Not good to use for hierarchical data storage Good to use for hierarchical data storage

Due to Vertically scalability –user can increase Due to Horizontally scalability – user can add
RAM more servers
Highlights on ACID properties (Atomicity, Highlights on CAP theorem (Consistency,
Consistency, Isolation and Durability) Availability and Partition tolerance)

Tools used are MongoDB, Cassandra,


Tools used are Microsoft SQL
CouchDB, Bigtable, FlockDB, ArangoDB, etc
Server, PostgreSQL, MySQL, Oracle, etc

Performance comparision between MongoDB and SQL

• SQL databases performance tuning consists of making queries of a relational


database run as fast as possible. Indexing in data structure improves the speed of
data retrieval operations on a database table by providing rapid random lookups
and efficient access of ordered records. It’s high-performing for complex queries.
• MongoDB Performance As you develop and operate applications with MongoDB,
you may need to analyse the performance of the application and its database.
When you come across tarnished performance, it is mostly due to database access
strategies or hardware availability or the number of open database connections.
It’s high-performing for simple query

MongoDB: which type of DB?

Mongo DB – Document Oriented NoSQL Database Approach

In MongoDB, Querying on this model is easy, since the schema is de-normalized. No joins
are required. So we plan to use the Mongo DB based solution

Introduction to MongoDB

MongoDB is an open source, document-oriented database that stores data in form of


documents (key and value pairs). As discussed in our last tutorial (NoSQL introduction)
that document based databases are one of types of NoSQL databases.

What is a document?

If you came from a relational database background then you can think of them as rows in
RDBMS. The mapping between relational database and MongoDB is covered in the next
tutorial so if you want to know the equivalent of rows, tables, columns in MongoDB, you
should definitely check it: Mapping Relational database to MongoDB. This is a JSON like
structure. Where data is stored in form of key and value pairs.
{

name: "Chaitanya",

age: 30,

website: "beginnersbook.com",

hobbies: ["Teaching", "Watching TV"]

History of MongoDB

MongoDB was created by Eliot and Dwight (founders of DoubleClick) in 2007, when they
faced scalability issues while working with relational database. The organization that
developed MongoDB was originally known as 10gen.

In Feb 2009, they changed their business model and released MongoDB as an open-
source Project. The organization changed its name in 2013 and now known as MongoDB
Inc.

Features of MongoDB

1. MongoDB provides high performance. Most of the operations in the MongoDB are
faster compared to relational databases.
2. MongoDB provides auto replication feature that allows you to quickly recover data
in case of a failure.
3. Horizontal scaling is possible in MongoDB because of sharing. Sharding is
partitioning of data and placing it on multiple machines in such a way that the
order of the data is preserved.
Horizontal scaling vs vertical scaling:

Vertical scaling means adding more resources to the existing machine while
horizontal scaling means adding more machines to handle the data. Vertical
scaling is not that easy to implement, on the other hand horizontal scaling is
easy to implement. Horizontal scaling database examples: MongoDB,
Cassandra etc.

4. Load balancing: Horizontal scaling allows MongoDB to balance the load.


5. High Availability: Auto Replication improves the availability of MongoDB
database.
6. Indexing: Index is a single field within the document. Indexes are used to quickly
locate data without having to search every document in a MongoDB database. This
improves the performance of operations performed on the MongoDB database.

Mapping Relational Databases to MongoDB

If you are coming from a relational database background then it might be difficult for you
to relate the RDBMS terms with MongoDB. In this guide, we will see the mapping between
relational database and MongoDB.
Collections in MongoDB is equivalent to the tables in RDBMS.

Documents in MongoDB is equivalent to the rows in RDBMS.

Fields in MongoDB is equivalent to the columns in RDBMS.

Fields (key and value pairs) are stored in document; documents are stored in collection
and

collections are stored in database.

This is how a document looks in MongoDB: As you can see this is similar to the row in

RDBMS. The only difference is that they are in JSON format.


Table vs Collection

Here we will see how a table in relational database looks in MongoDB. As you see columns
are represented as key-value pairs (JSON Format), rows are represented as documents.
MongoDB automatically inserts a unique id (12-byte field) field in every document, this
serves as primary key for each document.

Another cool thing about MongoDB is that it supports dynamic schema which means one
document of a collection can have 4 fields while the other document has only 3 fields. This
is not possible in relational database.
Experiment No: 01
BASIC OPERATIONS IN MONGODB

Aim:
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)

MongoDB Query Operations:


1. Where Clause:
The Where Clause in MongoDB is implemented using the find() method. It allows you to
filter documents based on certain conditions.
Syntax: db.collection.find({ field: value });
2. AND Operator:
To apply the AND operator in MongoDB queries, you can specify multiple conditions
within the same object.
Syntax: db.collection.find({ $and: [ { condition1 }, { condition2 } ] });
3. OR Operator:
Similarly, the OR operator can be used by providing multiple conditions within the $or
array.
Syntax: db.collection.find({ $or: [ { condition1 }, { condition2 } ] });

MongoDB Commands:
Insert:
To insert documents into a collection, you use the insertOne() or insertMany() method.
Syntax: db.collection.insertOne({ field1: value1, field2: value2 });
Query:
To retrieve documents from a collection, you use the find() method.
Syntax: db.collection.find({ field: value });
Update:
To update documents in a collection, you use the updateOne() or updateMany() method.
Syntax: db.collection.updateOne({ field: value }, { $set: { fieldToUpdate: newValue } });
Delete:
To delete documents from a collection, you use the deleteOne() or deleteMany()
method.
Syntax: db.collection.deleteOne({ field: value });
Projection:
Projection allows you to specify which fields to include or exclude in the query results.
Syntax: db.collection.find({ field: value }, { fieldToInclude: 1, _id: 0 });

Example:
Let's put all these operations together in an example: Suppose we have a collection
called users with documents representing users:
[
{"id": 1, "name": "Alice", "age": 30, "city": "New York"},
{"id": 2, "name": "Bob", "age": 25, "city": "Los Angeles"},
{"id": 3, "name": "Charlie", "age": 35, "city": "Chicago"}
]
Query:
Find users who are older than 25 and live in either New York or Chicago:
db.users.find({ $and: [ { age: { $gt: 25 } }, { $or: [ { city: "New York" }, { city: "Chicago" } ]
} ] });
This query will return documents for Alice and Charlie.

Update:
Update Bob's age to 27:
db.users.updateOne({ name: "Bob" }, { $set: { age: 27 } });
Delete:
Delete users who are younger than 30:
db.users.deleteMany({ age: { $lt: 30 } });

Projection:
Get only the names of users:
db.users.find({}, { name: 1, _id: 0 });
This will return:
[
{ "name": "Alice" },
{ "name": "Bob" },
{ "name": "Charlie" }
]
Experiment No: 02
Basic Operations

Aim:
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]

MongoDB Query Operations:


a.
Syntax: db.collectionName.find({}, { field1: 1, field2: 1, ... , fieldN: 1, _id: 0 })

Set the value to 1 for the fields you want to include and _id to 0 if you want to exclude the
default _id field.

For example, if you have a collection named users and you want to select only the name
and email fields while excluding the _id field, you can use the following query:

db.users.find({}, { name: 1, email: 1, _id: 0 })

This query will return all documents from the users collection with only the name and
email fields included.

b.
use the limit() method in MongoDB to limit the number of documents returned by a query.

Syntax: db.collectionName.find({}, { field1: 1, field2: 1, ... , fieldN: 1, _id: 0 }).limit(5)

continuing with the previous example of selecting name and email fields from the users
collection:
db.users.find({}, { name: 1, email: 1, _id: 0 }).limit(5)

This query will return the first 5 documents from the users collection, including only the
name and email fields, and excluding the _id field.
Experiment No: 03
Selectors

Aim:
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

MongoDB Query Operations:


a.
Let's say we have a collection named products with documents representing various
products.

Comparison Selectors:

// Find products with price less than $50


db.products.find({ price: { $lt: 50 } })

// Find products with price greater than or equal to $100


db.products.find({ price: { $gte: 100 } })

// Find products with quantity equal to 0


db.products.find({ quantity: { $eq: 0 } })

Logical Selectors:

// Find products with price less than $50 or quantity greater than 100
db.products.find({ $or: [ { price: { $lt: 50 } }, { quantity: { $gt: 100 } } ] })

// Find products with price greater than or equal to $100 and quantity less than or equal
to 10
db.products.find({ $and: [ { price: { $gte: 100 } }, { quantity: { $lte: 10 } } ] })

b.

Geospatial Selectors:
Suppose we have a collection named locations where each document represents a
location with coordinates.
// Create a 2d sphere index on the 'location' field for geospatial queries
db.locations.createIndex({ location: "2dsphere" })

// Find locations near a specific point (longitude, latitude) within a specified distance (in
meters)
db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [longitude, latitude]
},
$maxDistance: distanceInMeters
}
}
})

Replace longitude, latitude, and distanceInMeters with the coordinates and distance you
want to search around.

Bitwise Selectors:
Suppose we have a collection named permissions where each document represents a
user's permissions stored as bit flags.

// Find users with read and write permissions


db.permissions.find({ $bitsAllSet: { permissions: <bitmask> } })

// Find users with at least one of the specified permissions


db.permissions.find({ $bitsAnySet: { permissions: <bitmask> } })

// Find users without a specific permission


db.permissions.find({ $bitsAllClear: { permissions: <bitmask> } })

Replace <bitmask> with the binary representation of the permissions you want to query
for.

Output
Experiment No: 04
Projection Operators
Aim:
Create and demonstrate how projection operators ($, $elematch and $slice) would be
used in the MongoDB.

MongoDB Query Operations:

let's first create a sample collection named orders with some documents representing
orders, each containing an array of items:
db.orders.insertMany([
{
order_id: 1,
customer_name: "Alice",
items: [
{ name: "iPhone", quantity: 2 },
{ name: "Laptop", quantity: 1 },
{ name: "Headphones", quantity: 3 }
]
},
{
order_id: 2,
customer_name: "Bob",
items: [
{ name: "Tablet", quantity: 1 },
{ name: "Smartwatch", quantity: 2 }
]
}
])

Now let's demonstrate how projection operators work:


$ Projection Operator: This operator is used to project elements in an array.
// Retrieve only the names of items from the first order
db.orders.find({ order_id: 1 }, { "items.name": 1, _id: 0 })

$elemMatch Projection Operator: This operator is used to project specific elements


that match certain criteria within an array.
// Retrieve only the items from the first order where quantity is greater than 2
db.orders.find({ order_id: 1 }, { items: { $elemMatch: { quantity: { $gt: 2 } } }, _id: 0 })

$slice Projection Operator: This operator is used to project a subset of elements in an


array.
// Retrieve only the first two items from the first order
db.orders.find({ order_id: 1 }, { items: { $slice: 2 }, _id: 0 })

Output
Experiment No: 05
Aggregation Operations

Aim:
Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.). students
encourage to execute several queries to demonstrate various aggregation operators)

MongoDB Query Operations:

Let's say we have a collection named "students" with documents structured like this:

{
"_id": 1,
"name": "John",
"age": 20,
"grade": "A"
}

Aggregation operations:

$avg: Calculates the average age of all students.

db.students.aggregate([
{
$group: {
_id: null,
averageAge: { $avg: "$age" }
}
}
])

$min: Finds the minimum age among all students.

db.students.aggregate([
{
$group: {
_id: null,
minAge: { $min: "$age" }
}
}
])
$max: Finds the maximum age among all students.

db.students.aggregate([
{
$group: {
_id: null,
maxAge: { $max: "$age" }
}
}
])

$push: Groups students by their grades and pushes their names into an array for
each grade.

db.students.aggregate([
{
$group: {
_id: "$grade",
students: { $push: "$name" }
}
}
])

$addToSet: Similar to $push, but ensures unique values in the resulting array.

db.students.aggregate([
{
$group: {
_id: "$grade",
uniqueStudents: { $addToSet: "$name" }
}
}
])

Output
Experiment No: 06
MongoDB Aggregation Pipeline

Aim:
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)

MongoDB Query Operations:

MongoDB's aggregation pipeline allows for more complex data transformations by


combining multiple stages. Here's an example pipeline containing various stages like
$match, $group, $sort, $project, and $skip.

Consider the same "students" collection:


[
{ "_id": 1, "name": "John", "age": 20, "grade": "A" },
{ "_id": 2, "name": "Alice", "age": 22, "grade": "B" },
{ "_id": 3, "name": "Bob", "age": 21, "grade": "A" },
{ "_id": 4, "name": "Eva", "age": 23, "grade": "C" },
{ "_id": 5, "name": "Michael", "age": 20, "grade": "A" }
]

Let's construct a pipeline:

$match: Filters documents by a condition (e.g., students aged 20).


$group: Groups documents by a specified expression (e.g., grade), and calculates
aggregate values for each group.
$sort: Sorts documents based on specified fields.
$project: Reshapes documents by including, excluding, or renaming fields.
$skip: Skips a specified number of documents.
Here's how the pipeline would look:

db.students.aggregate([
{
$match: { age: 20 }
},
{
$group: {
_id: "$grade",
count: { $sum: 1 },
averageAge: { $avg: "$age" }
}
},
{
$sort: { count: -1 }
},
{
$project: {
_id: 0,
grade: "$_id",
count: 1,
averageAge: 1
}
},
{
$skip: 1
}
])

Explanation:

$match: Filters documents where the age is 20.

$group: Groups documents by grade, counting the number of students in each grade and
calculating the average age.

$sort: Sorts the documents by the count of students in descending order.

$project: Reshapes the output documents to include only the grade, count, and average
age.
$skip: Skips the first result.

This pipeline will give you aggregated data about students aged 20, grouped by grade,
sorted by the count of students in each grade in descending order, and skipping the first
result.

Output
Experiment No: 07
MongoDB Aggregation Pipeline
Aim:
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.

MongoDB Query Operations:

To 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, you can use the MongoDB
aggregation pipeline with the $lookup stage to join the "Listings" and "Hosts" collections.

db.listings.aggregate([
{
$lookup: {
from: "hosts",
localField: "host_id",
foreignField: "_id",
as: "host"
}
},
{
$match: {
"host.host_picture_url": { $exists: true, $ne: null }
}
},
{
$project: {
_id: 0,
listing_url: 1,
name: 1,
address: 1,
host_picture_url: "$host.host_picture_url"
}
}
])

Explanation:
- $lookup: This stage performs a left outer join to the "Hosts" collection based on the
host_id field in the "Listings" collection.
- $match: Filters documents where the host_picture_url exists and is not null.
- $project: Reshapes the output documents to include only the required fields from the
"Listings" collection and the host_picture_url from the "Hosts" collection.

To display reviews summary using an e-commerce collection, you can use MongoDB
aggregation functions to calculate various summary statistics like average rating, total
reviews count, etc. Here's an example query:

db.products.aggregate([
{
$project: {
_id: 0,
name: 1,
average_rating: { $avg: "$reviews.rating" },
total_reviews: { $size: "$reviews" },
five_star_reviews: {
$size: {
$filter: {
input: "$reviews",
cond: { $eq: ["$$this.rating", 5] }
}
}
},
one_star_reviews: {
$size: {
$filter: {
input: "$reviews",
cond: { $eq: ["$$this.rating", 1] }
}
}
}
}
}
])

Explanation:
$project: Projects the required fields and performs calculations on the reviews array.
average_rating: Calculates the average rating using $avg.
total_reviews: Counts the number of reviews using $size.
five_star_reviews: Counts the number of reviews with a rating of 5 using $filter and size.
one_star_reviews: Counts the number of reviews with a rating of 1 using $filter and
$size.
Output
Experiment No: 08
Indexes in MongoDB

Aim:
a. Demonstrate creation of different types of indexes on collection (unique, sparse,
compound and multikey indexes)
b. Demonstrate optimization of queries using indexes.

MongoDB Query Operations:

Creation of Different Types of Indexes on Collection

Unique Index: Ensures that the indexed fields have unique values across the collection.
db.collection.createIndex({ "email": 1 }, { unique: true })

Sparse Index: Indexes only documents that contain the indexed field, omitting
documents that lack the indexed field.
db.collection.createIndex({ "age": 1 }, { sparse: true })

Compound Index: Indexes multiple fields together. Useful for queries that involve
multiple fields.
db.collection.createIndex({ "name": 1, "age": -1 })

Multikey Index: Indexes arrays, allowing queries to efficiently match elements within
arrays.
db.collection.createIndex({ "tags": 1 })
Optimization of Queries Using Indexes:

Optimizing queries involves identifying which fields are commonly used in queries and
creating indexes on those fields. Let's consider a scenario where we have a collection of
books and we frequently query based on the author's name and the publication year.

// Create indexes
db.books.createIndex({ "author": 1 })
db.books.createIndex({ "publication_year": 1 })

// Query optimization
// Example 1: Query by author
db.books.find({ "author": "J.K. Rowling" })

// Example 2: Query by publication year


db.books.find({ "publication_year": 2005 })

// Example 3: Query by both author and publication year


db.books.find({ "author": "J.K. Rowling", "publication_year": 2005 })

In the above examples, we create indexes on the fields "author" and "publication_year".
These indexes will significantly speed up queries that involve filtering by these fields.

When executing queries, MongoDB's query optimizer will automatically select the most
efficient index to use based on the query predicates. By creating appropriate indexes, we
can ensure that queries execute efficiently, improving overall database performance.

Output
Experiment No: 09
Text Search

Aim:
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

MongoDB Query Operations:

Text Search Using Catalog Data Collection:


We have a collection named "catalog" containing product data with a field named
"description" where we want to perform text searches. Here's how you can develop a
query to demonstrate text search for a given word:

// Create a text index on the "description" field


db.catalog.createIndex({ description: "text" })

// Perform a text search for a given word


var searchWord = "phone"
db.catalog.find({ $text: { $search: searchWord } })

Explanation:
• We create a text index on the "description" field to enable text search.
• We perform a text search using the $text operator and the $search operator. This
query will return all documents where the "description" field contains the word
"phone".

Excluding Documents with Certain Words and Phrases:

Let's say we want to exclude documents that contain certain words or phrases from our
search results. We can achieve this using the $not operator combined with regular
expressions.

// Find documents not containing the word "sale" in the description


db.catalog.find({ description: { $not: /sale/i } })

// Find documents not containing the phrase "out of stock" in the description
db.catalog.find({ description: { $not: /out of stock/i } })
Explanation:

• We use the $not operator to negate the regular expression pattern.


• In the first query, we find documents where the "description" field does not
contain the word "sale".
• In the second query, we find documents where the "description" field does not
contain the phrase "out of stock". The "i" flag makes the regular expression case-
insensitive.

Output
Experiment No: 10
Text Search

Aim:
Develop an aggregation pipeline to illustrate Text search on Catalog data collection.

MongoDB Query Operations:

To perform text search using the aggregation pipeline in MongoDB, we can use the
$match stage along with the $text operator. Here's how you can develop an aggregation
pipeline to illustrate text search on the "catalog" data collection:

db.catalog.aggregate([
{
$match: {
$text: {
$search: "phone"
}
}
}
])

Explanation:

• We use the $match stage to filter documents based on the text search criteria.
• Within the $match stage, we use the $text operator to perform the text search.
• The $search parameter specifies the word or phrase to search for. In this case,
we're searching for the word "phone".

This aggregation pipeline will return all documents from the "catalog" collection where
the "description" field contains the word "phone". Make sure you have created a text
index on the "description" field in the "catalog" collection before executing this
aggregation pipeline for efficient text search.

Output

You might also like