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

MongoDB

Uploaded by

sushmasushi2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

MongoDB

Uploaded by

sushmasushi2005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 99

Introduction To MongoDB

Types of Databases

Depending on the way we organize data they are different types of


databases.

Non-Relational Databases (NoSQL)


Why NoSQL?

NoSQL is also referred to as Not Only SQL

The two most important advantages that NoSQL databases provide are:

 Flexibility

 High Scalability

Let's take a Document Database as an example, where data is stored as


JSON-like objects instead of tables & rows in a relational database

Actually MongoDB stores data in BSON format, which allows it to be


traversed much more quickly compared to JSON. BSON stands for Binary
JSON

Relational Database:

Name Age

Joe 25

Document Database:
1

"name": "Joe",

"age": 25

JSON

Flexibility

For example, we are storing user details of an e-commerce application in a


Document database.

First storing the subscription expiry date of a prime user, we can add new
keys to documents as and when required.

We need not redesign tables(collections in MongoDB) & reorganizing data


for each and every change.

Non-Prime user

"name": "Joe",
"age": 25

JSON

Prime user

"name": "Sam",

"age": 25,

"expiry_date": "01-01-2022"

JSON

Unlike relational databases, We can store related data in a single


document.

This makes querying simpler & faster as it doesn’t involve any join
operations.

Relational Databases:
Non-Relational Databases:

10

11

{
"user_id": 1,

"name": "Joe",

"age": 25,

"gender": "F",

"cart": [

"product_id": 10,

"quantity": 2

...

JSON

Expand

High Scalability

On a sale day like the Big Billion Day, the number of requests related to
various purchases and products increases enormously.

Ideally, we would like to have our database serve the additional load
without any issue

Scalability

The ability to serve the increasing number of incoming requests/data if


necessary resources are given.

Built for Scale

NoSQL databases are designed to address scalability and performance


issues that traditional relational databases encounter.

Various strategies which are followed are:

1. Support Horizontal Scaling

2. Sharding

3. Relaxation on ACID properties etc.

Advantages:

 NoSQL databases are highly adopted in the industry


 Document database is one of the most commonly used NoSQL
databases

MongoDB

MongoDB is one of the most popular document databases and is used in


popular stacks like MERN & MEAN, and it is a NoSQL document database.

Use cases:

 Real-time Analytics

 Gaming

 Mobile Application Development

 Internet of Things

Used By: MongoDB is used in the below companies

Collection

The group of documents is called as Collection


RDBMS vs MongoDB

Comparing the terminology and concepts used in Relational Database


Management Systems (RDBMS) with those used in MongoDB.

Table vs Collection:

In RDBMS, data is stored in Tables, which are structured with fixed


schemas - each table has a defined set of columns and each row in the
table is a record.
In MongoDB, the equivalent structure is called a Collection. Unlike tables,
collections do not require a fixed schema, meaning that the documents
(records) within a collection can have different fields.

In MongoDB, a field is a term that refers to a name-value pair in a


document. Documents are composed of these fields, which are similar to
columns in a relational database. Each field in a MongoDB document is a
piece of data or an element with a name (also known as a "key") and a
value.

Choosing a Database

Relational and non-relational databases solve different problems so when


picking a DBMS choose the one that best solves the problem.

Submit Feedback

Getting Started with MongoDB

Setting up MongoDB

To get started with MongoDB and create databases in it, we need to


perform the following steps:

1. Create MongoDB Cluster

2. Install MongoDB Shell

3. Connect to MongoDB

1. Create MongoDB Cluster


Creating a MongoDB cluster involves:

 Creating MongoDB account

 Creating Cluster

Creating MongoDB account:

 Open

Google

and search for

mongodb login

 In the Login page choose your Google account to create a MongoDB


account.

 Accept the Privacy policy and terms of Service and click on the
submit button.

 The welcome page will be visible for a few seconds, wait for the
main page.

 On the main page we need to fill in some details

o Select

Learn MongoDB

from the dropdown.

o Select

New to MongoDB

o Select language as

JavaScript / Node.js

o Select

Customer / user profile data

for kind of data.

o Select

Not sure / None


for Architectural models

o Click on the

Finish

button.

Creating Cluster:

 After clicking on the

Finish

button it will redirect to the next page

 Till now we have created a MongoDB Account,now, we need to


create MongoDB Database.

 In the Deploy your database page select the

Free

plan and click on the


Create

button.

 Next, we will redirect to the

Security Quickstart

page.

o In the first step select

Username and Password

 Enter your Username in the

Username

field.

 Enter the Password in the

Password

field.

o In the second step select

My Local Environment

 Enter 0.0.0.0 in the

IP Address

field.

 Click on the

Add Entry

button.

 Click on the

Finish and Close

button.

 Now

MongoDB Cluster

is created.
2. Install MongoDB Shell

2.1. Install MongoDB Shell on Windows

Follow the below steps to download & MongoDB Shell:

 Click here and download the MongoDB Shell.


 Choose the platform based on your system and click on the
download button to download the MongoDB Shell package zip.

 Move the unzipped folder to the

C: drive
 Now click on the

Start

menu in our system and search for the

environmental variables

 Environment variables are like special instructions that tell the


computer where to find important programs and files.

 Open the

Edit the system environment variables

.
 In the opened window, Click on the

Environment variables

button
 Go to the

System variables

panel. Select the

Path

option and click the

Edit

button.
 Create a new variable and add the path of the bin folder, now click
on the

New

button and then click the

Browse

button to select the downloaded MongoDB shell path.


 Now select the path of the

bin

folder in the downloaded MongoDb shell folder in the C drive of the


system.
 Now the new path will be added, click on the

Ok

button. We have added the path successfully.

 Run

mongosh --version

command in the terminal to check if the MongoDB shell has been


successfully installed or not.

3. Connect to MongoDB

 Connect to the Database using Shell. Get the connection string from
the website and execute it in the terminal as shown below.
 Enter the user password and press the enter key.

 In order to get the available databases, we can use the

show dbs

command.

Creating a Database

 To create a database, we can use:

use <databaseName>

command

 This command is generally used to switch between databases, how


this will create a new database if it does not exist.

 To create a collection, we use

createCollection()
 Unlike in Relational Databases, we need not specify the fields
beforehand.

 Retrieve all the collections from the database.


Submit Feedback

Querying MongoDB - Part 1

CRUD Operations

Insert Document

To insert a document into the product collection, we use the

insertOne()

function.

Syntax

db.collection.insertOne({...})

SHELL

SQL Vs MongoDB:
As MongoDB is flexible with the structure of the document, we can insert
JSON with available fields.

db.product.insertOne(

name: "Bat",

price: 2999,

brand: "Yonex",

category: "Sport"

SHELL

Note: Here we haven’t provided any

discount_in_percentage

information.
Unique Id:

_id

is the primary key, which is automatically generated by MongoDB or can


be explicitly passed when creating a document.

Output:

_id: ObjectId("6098fb...")

name: "Bat",

price: 2999,

brand: "Yonex",

category: "Sport"

SHELL

Read Document

To read documents from a collection, we use the

find()

function

Syntax
1

db.collection.find()

SHELL

It is similar to selecting all the columns in a database

using * in SQL

To retrieve all the documents in the

product

collection use the

find()

function.

Output:

10

11

{
_id: ObjectId("608a3a..."),

name: "Smart watch",

price: 22995,

category: "Watches",

brand: "Fossil",

discount_in_percentage: 10

},

_id: ObjectId("6098fb..."),

SHELL

Expand

Insert Multiple Documents

To insert multiple documents we use the

insertMany()

function.

Syntax

db.collection.insertMany([{...}])

SHELL

Retrieve Specific Documents

Similar to the

WHERE

clause in SQL, we can optionally specify conditions in

find()

.
Update Document

This is very similar to SQL, to update documents in a collection use the

updateMany()

function.

Update price of

"Bat"

to 2500:

MongoDB:

1
2

db.product.updateMany(

{name: "Bat"},

{$set: {price: 2500}}

SHELL

SQL

UPDATE product

SET price = 2500

WHERE name = "Bat";

SQL

Update All Documents

Set the

discount_in_percentage

of all the products to 0.

MongoDB:

db.product.updateMany(

{},
{

$set: {"discount_in_percentage": 0}

SHELL

SQL

UPDATE product

SET discount_in_percentage=0;

SQL

Here we have used

$set

operator in MongoDB. This operator will set the value of the field with
specified value, if the specified field is not present it will add the new field

Delete Document

We can use

deleteMany()

to delete documents.

MongoDB:

db.product.deleteMany(

condition

SHELL

SQL

1
2

DELETE FROM product

WHERE condition;

SQL

If we want to delete all the documents ina collection we can use empty
brases

{}

in the place of condition.

Summary of CRUD Operations

Syntax:

Function names and their operations:

Projection

How to get the only specific fields from Documents?

We can use MongoDB projection, which can be used to extract only the
fields you need from a document.

In order to get the field in the result we need to specify the field name as
key and value as 1. Similarly if we want to exclude a field, we need to
specify the value as 0

Retrieve Specific Field:

Get the

id

name

, and

price

of all products which belong to Electronics.


MongoDB:

db.product.find(

{category: "Electronics"},

{name: 1, price: 1, _id: 1}

SHELL

SQL

SELECT id, name, price

FROM product

WHERE category = "Electronics";

SQL

Excluding Fields:

Get all fields except the category of all products that belong to the
Electronics category.

MongoDB:

db.product.find(

{category: "Electronics"},

{category: 0}
)

SHELL

Inclusion and Exclusion:

db.product.find(

condition,

{field1: flag, field2: flag, ...}

SHELL

Projection F

Inclusion

Exclusion

Common Mistake

We cannot combine inclusion and exclusion statements in projection


documents.

Example:

db.product.find(

{category: "Electronics"},

{category: 0, name: 1}

)
C

Submit Feedback

Querying MongoDB - Part 2

Conditional Operators

We can use conditional operators to filter the documents retrieved.

Syntax:

db.collection.find(

{field_name: { operator: value}}

SHELL

Operator(SQL) Vs Operator(MongoDB):

Operator(SQL) Operator(MongoDB)

= $eq

> $gt

>= $gte

< $lt
Operator(SQL) Operator(MongoDB)

=< $lte

Examples:

 Belongs to the Clothing category.

{category: { $eq: "Clothing"}}

SHELL

 Price less than or equal to 1000.

{price: { $lte: 1000}}

SHELL

 Discount greater than 10.

{discount_in_percentage: { $gt: 10}}

SHELL

 For equality condition, we can skip the operator

$eq

operator.

Query:

db.product.find(

{category: "Sport"},

SHELL

Output:

1
2

10

_id: ObjectId("608a3c..."),

name: 'Bat',

price: 2999,

brand: Yonex,

category: 'Sport'

},

...

SHELL

Ordering Results

Sort Function

In MongoDB, we use the

sort()

function to sort the results.

MongoDB:

db.collection_name.find().sort({field_name: order})

SHELL
SQL

SELECT *

FROM table_name

ORDER BY field_name order;

SQL

SQL Vs MongoDB:

SQL MongoDB

ASC 1

DESC -1

Example:

Get all the products in descending order of price.

MongoDB:

db.product.find().sort({price:-1})

SHELL

SQL

SELECT *

FROM product

ORDER BY price DESC;

SQL

Common Mistake

The sort method should come after the


find()

function.

db.product.sort(

{price:-1}

).find()

SHELL

Sorting on Multiple Fields

Ordering on multiple fields will happen in the order of fields given.

 Descending order of price

 Ascending order of name

MongoDB:

db.product.find().sort({price:-1, name:1})

SHELL

SQL

SELECT *

FROM product

ORDER BY

price DESC,

name ASC;

SQL
Pagination

Skip & Limit Functions

We use

limit()

and

skip()

for pagination in MongoDB.

db.product.find().skip(n).limit(m)

SHELL

 n = Skips

documents

 m = Selects

documents

SQL Vs MongoDB:

SQL MongoDB

OFFSET skip

LIMIT limit

Example:

 Get next(after first 5) 10 best rated products.

MongoDB:

db.product.find().sort(

{rating:-1}
).skip(5).limit(10)

SHELL

SQL

SELECT *

FROM product

ORDER BY rating DESC

LIMIT 10 OFFSET 5;

SQL

Logical Operators

To perform Logical operations,

We have three logical Operators

 AND ($and)

 OR ($or)

 NOT ($not)

we wrap conditions in

[]

Syntax:

db.collection.function(

{ operator: [ cond_1, cond_2 ] }

SHELL
SQL Vs MongoDB:

SQL MongoDB

AND $and

OR $or

NOT $not

AND Operator

Find all products that belongs to

 Puma and

 Price less than or equal to 1500

MongoDB:

db.product.find({
$and: [

{brand: "Puma"},

{price:{$lte: 1500}}

})

SHELL

AND & OR

Find all products that belongs to

 Puma or Denim brands and

 Price less than or equal to 1500

MongoDB:

db.product.find({

$and: [

$or: [{brand: "Puma"},

{brand: "Denim"}]

},

{price:{$lte: 1500}}

})
SHELL

Query Walkthrough

 Find all products with discounts greater than 10

Query:

db.product.find({

discount_in_percentage:{$gt: 10}

})

SHELL

 Find all products with

o discount greater than 10

o And the output should be in the ascending order of price

Query:

db.product.find({

discount_in_percentage:{$gt: 10}

}).sort({price: 1})

SHELL

 Top 10 products with

o discount greater than 5

o when ordered in the ascending order of price

Query:

3
db.product.find({

discount_in_percentage:{$gt: 5}

}).sort({price: 1}).limit(10)

SHELL

 Get next 5(after 10 retrieved in the previous question) products

Query:

db.product.find({

discount_in_percentage: {$gt: 5}

}).sort({price: 1}).skip(10).limit(5)

SHELL

Submit Feedback

Grouping & Aggregations

Aggregate

To perform aggregation & grouping operations on MongoDB we use the

aggregate

function.

Syntax:

db.collection.aggregate([...]);

SHELL

Group By

Group By Syntax:
Find the total score of each player.

Solving this query requires performing SUM(score) aggregation for each of


the players i.e

GROUP BY

name

MongoDB:

db.playerMatchDetails.aggregate([

$group:{_id: '$name',

totalScore: {$sum: '$score'}}

]);

SHELL

SQL

4
SELECT

name, SUM(score) as totalScore

FROM player_match_details

GROUP BY name;

SQL

Comparision of SQL & MongoDB Syntaxes:

Query SQL Mong

Group By Name GROUP BY name grou

Sum score for each player SUM(score) as totalScore tota

Simple Aggregation

The average score of all the players

 If we specify an _id value of null, or any other constant value, the

$group

stage returns a single document that aggregates values across all of all
the documents.

MongoDB:

db.players.aggregate([{

$group:{

_id: null,

avgScore: {$avg: '$score'}}

}]);

SHELL
SQL

SELECT

AVG(score) as avg_score

FROM player_match_details

SQL

Other Aggregations:

Aggregation Operator MongoDb

Sum $sum

Avg $avg

Max $max

Min $min

Query Walkthrough

Example I:

 Get the total number of fours scored by each player.

Query:

db.playerMatchDetails.aggregate([
{

$group: {

_id: '$name',

totalFours: {$sum: '$noOfFours'

]);

SHELL

Example II:

 Get the highest score of an individual.

Query:

db.playerMatchDetails.aggregate([

$group: {

_id: null,

highestScore: {$max: '$score'}

]);

SHELL
Example III:

 Get the highest score for each player.

Query:

db.playerMatchDetails.aggregate([

$group: {

_id: '$name',

highestScore: {$max: '$score'}

]);

SHELL

Aggregation on Selected Documents

Example:

 Avg. score of each player in the year 2020

Approach:

This involves two steps,

1. Filtering the documents with the year 2020.

2. Performing group by & aggregation.

Stages:

We can specify each such step in aggregate function as stages.


Syntax

db.playerMatchDetails.aggregate([

stage1,

stage2,

...

]);

SHELL

Operators:

The behavior/Functionality of each step/stage is specified by

operators

, i.e.

$group

$match

$sort

$skip

$limit

etc.

Choosing Operators
Stages Operator Description

filter $match Filter documents for next sta

Group By $group Perform group by and aggre

 We use

$match

to filter the documents. It takes conditions.

Solution:

 Avg. score of each player in the year 2020

Query:

db.playerMatchDetails.aggregate([

$match: { year: 2020 }

},

$group:{_id:'$name',avgScore:{$avg:'$score'}}

]);

SHELL

Order of Stages:
 The order of stages is important as the output of stages is passed as
input for the next stage.

Query:

db.playerMatchDetails.aggregate([

$group:{_id:'$name', ...}

},

{$match: { year: 2020 }}

]);

SHELL

Output:

[]

SHELL

 As the output for the first stage doesn’t have a year field, the
second stage results in an empty list.

Aggregation pipeline

Example:

 Retrieve players whose average score in the year 2021 is greater


than 40.

Approach:

This involves three steps i.e,

1. Filtering the documents with year 2021


2. Performing group by & aggregation

3. Filter the aggregated documents

Selecting Operators:

Aggregation:

MongoDB:

10

11

12

13

14

15

16
17

18

db.playerMatchDetails.aggregate([

$match: {

year: 2021

},

$group: {

_id: '$name',

avgScore: {$avg: '$score'}

},

$match: {

avgScore: {$gt: 40}

]);

SHELL

Collapse
This framework of aggregating data using a sequence of stages is called
an

aggregation pipeline

Query Walkthrough

Example I :

 Get the total number of sixes scored by each player.

Performing group by & aggregation

$group:{

_id: '$name', totalSixes:{$sum: '$noOfSixes'}

SHELL

Solution:
1

db.playerMatchDetails.aggregate([

$group: {

_id: '$name',

totalSixes: {$sum: '$noOfSixes'}

]);

SHELL

Example II :

 Get the total number of sixes scored by each player in the year
2021 and also consider only T20 matches.

Filtering the documents with the year 2021

$match: {

year: 2021, matchType: 'T20'

SHELL

Performing group by & aggregation


1

$group:{

_id: '$name',

totalSixes: {$sum: '$noOfSixes'}

SHELL

Solution:

10

11

12

13

14

db.playerMatchDetails.aggregate([

$match: {

year: 2021, matchType: 'T20'

}
},

$group: {

_id: '$name',

totalSixes: {$sum: '$noOfSixes'}

]);

SHELL

Collapse

Example III :

 Get the players who hit at least 5 sixes

o in the year 2021 and

o also consider only T20 matches

Filter required matches

$match: {

year: 2021, matchType: 'T20'

SHELL

Performing group by & aggregation

$group:{

_id: '$name',
totalSixes: {$sum: '$sixes'}

SHELL

Filter updated documents

$match: {totalSixes: {$gt: 5}}

SHELL

Solution:

10

11

12

13

14

15

db.playerMatchDetails.aggregate([

$match: {year: 2021, matchType: 'T20'}

},

$group: {
_id: '$name',

totalSixes: {$sum: '$noOfSixes'}

},

$match: {totalSixes: {$gt: 5}}

]);

SHELL

Collapse

Submit Feedback

Complex Field Types

Nested Document

We have a sample database that includes

Product

and

User

Collection documents.

Product Collection :

name:"Smart watch",

price:22995,
category:"Watches",

brand:"Fossil",

discountInPercentage: 10

SHELL

User Collection :

name: "Joe",

age: 25,

gender: "M"

SHELL

Let’s update the Rating feature in our e-commerce app. Our database
should store the following details for each product:

 Average Ratings

 Number of users rated


 In Relational Database:

o One product is associated with one rating details

 In Document Database:
 Nested Documents are also called

Embedded Documents

CRUD Operations

Basic CRUD operations are very much similar to handling any document.

 insertOne

 updateMany

 insertMany

 deleteMany

Querying Nested Documents:

Example:

 Get products with

o Average rating 3.5.

o The number of users rated is 1000.

Query:

5
6

db.product.find({

ratings: {

avgRating: 3.5,

noOfUsersRated: 1000

})

SHELL

Common Mistakes

Note: Order of the fields should be same as the one mentioned in the
document.

Dot(.) notation

 Dot notation in MongoDB is used to access the elements of nested


documents.

 If we have a document with nested fields, we can use dot notation


to access these fields.

Example I :

 Get products with

o No of users rated greater than or equal to 1000


Query:

db.product.find(

"ratings.noOfUsersRated": {$gte: 1000}

SHELL

Example II :

 Get products with

o Average rating 3.5

o No of users rated greater than or equal to 1000


Query:

db.product.find(

"ratings.avgRating": 3.5,

"ratings.noOfUsersRated": {$gte: 1000}

SHELL

Note: Field with

Dot(.)

notation must be double quoted.

Query Walkthrough

Example I :

 Find all best rated(i.e average rating >= 4) products

Query:

db.product.find(

{"ratings.avgRating": {$gte: 4}}

SHELL

Example II :
 Find all best rated(i.e average rating >= 4) products which belongs
to

o Mobiles category

Query:

db.product.find({

$and: [

{category: "Mobiles"},

{"ratings.avgRating": {$gte: 4}}

})

SHELL

Example III :

 Find all best rated(i.e average rating >= 4) products which belongs
to

o Mobiles category and

o Sort average rating in descending order

Query:

db.product.find({
$and: [

{category: "Mobiles"},

{"ratings.avgRating": {$gte: 4}}

}).sort({"ratings.avgRating": -1})

SHELL

Array Field

User Collection :

name: {

first: "Joe",

last: "Jonas"

age: 25,

gender: "M"

SHELL

Let’s add a Wish List feature to our e-commerce app:

 User

 Product

In the Relational Database need to create a new table wish list with
user_id

and

product_id

In the Document Database, We can add a wish list in the

user

documents itself.

MongoDB

{
name: {

first: "Joe",

last: "Jonas"

},

age: 25,

gender: "M",

wishList: [101, 102, 103]

SHELL

name id

Ear phones 101

one plus 9R 102

case cover 103

Query Walkthrough

Example I :

 Users with one plus 9R(id is 102) as one of the wish list.

Query

db.user.find({wishList:102})

SHELL

Example II :

 Users with one plus 9R(id is 102) and case cover(id is 103) in their
wish list.

Query

4
5

db.user.find({

wishList:{

$all: [102, 103]

})

SHELL

Note: The

$all

operator selects the documents where the value of a field is an array that
contains all the specified elements.

Example III :

 Users with one plus 9R(id is 102) or case cover(id is 103) in their
wish list.

Query

db.user.find({

wishList:{

$in: [102, 103]

})

SHELL

 choose the

$in

operator rather than the

$or
operator when performing equality checks on the same field.

 The

$in

operator selects the documents where the value of a field equals any
value in the specified array.

Example IV :

 Users with zero wish list.

Query

db.user.find({wishList:{$size: 0}})

SHELL

 The

$size

operator matches any array with the number of elements specified by the
argument.

Multiple Conditions

 Let’s add a Purchase History feature to our e-commerce app:

o Products

o Quantity

We can add purchase history in user documents as an array of nested


documents.

User Collection:

8
9

10

...

purchaseHistory: [

productId: 201,

quantity: 1,

...

SHELL

Example I:

 Get users who purchased

o Laptop(id is 201) and

o quantity of more than one

MongoDB

db.user.find(

"purchaseHistory.productId": {$eq: 201},

"purchaseHistory.quantity": {$gt: 1}

}
)

SHELL

But, here we get users whose products in purchase history match any one
of the conditions.

Element Match

The

$elemMatch

operator matches documents that contain an array field with at least one
array element that satisfies all the specified criteria.

MongoDB

db.user.find({

"purchaseHistory": {

$elemMatch: {
productId: 201,

quantity: {$gt: 1}

})

SHELL

Query Walkthrough

Example I :

 Find all users who purchased

o N95 Mask(id is 249)

Query

db.user.find(

{"purchaseHistory.productId": 249}

SHELL

Example II :

 Find all users who purchased


o N95 Mask(id is 249)

o Sanitiser(id is 266)

Query

db.user.find(

{"purchaseHistory.productId": {$all: [249, 266]}}

SHELL

Example III :

 Find all users who purchased

o More than 10 N95 Mask(id is 249)

Query

db.user.find({

"purchaseHistory": {

$elemMatch: {

productId: 249,

quantity: {$gt: 10}

}
})

SHELL

Submit Feedback

Top 50 MongoDB Interview Questions with Answers for 2024

Last Updated : 21 Oct, 2024

Getting ready for a MongoDB interview? MongoDB is a


popular NoSQL database known for
its flexibility and scalability by making it a favorite among many
developers and companies. Whether you're a beginner looking to land
your first job or an experienced professional aiming to advance your
career, being well-prepared for your interview is crucial.

This blog post compiles a comprehensive list of MongoDB interview


questions that will help you understand what to expect and how to tackle
them confidently. Dive in and start preparing for a successful MongoDB
interview.

Table of Content

 MongoDB Basic Interview Questions

 MongoDB Intermediate Interview Questions

 MongoDB Query Based Interview Questions


MongoDB Basic Interview Questions

MongoDB Basic Interview Questions focus on the essential knowledge


you need to get started with this NoSQL database. These questions
usually cover the differences between SQL and NoSQL databases, how
MongoDB stores data in documents, and basic operations like creating,
reading, updating, and deleting data.

1. What is MongoDB, and How Does It Differ from Traditional SQL


Databases?

MongoDB is a NoSQL database which means it does not use


the traditional table-based relational database structure. Instead of it
uses a flexible and document-oriented data model that stores data
in BSON (Binary JSON) format.

Unlike SQL databases that use rows and columns, MongoDB stores data
as JSON-like documents, making it easier to handle unstructured data and
providing greater flexibility in terms of schema design.

2. Explain BSON and Its Significance in MongoDB.


BSON (Binary JSON) is a binary-encoded serialization format used by
MongoDB to store documents. BSON extends JSON by adding support for
data types such as dates and binary data and it is designed to be efficient
in both storage space and scan speed. The binary format allows MongoDB
to be more efficient with data retrieval and storage compared to text-
based JSON.

3. Describe the Structure of a MongoDB Document.

A MongoDB document is a set of key-value pairs similar to a JSON object.


Each key is a string and the value can be a variety of data types including
strings, numbers, arrays, nested documents and more.

For example:

{
"_id": ObjectId("507f1f77bcf86cd799439011"),
"name": "Alice",
"age": 25,
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA"
},
"hobbies": ["reading", "cycling"]
}

4. What are Collections And Databases In MongoDB?

In MongoDB, a database is a container for collections, and a collection is a


group of MongoDB documents. Collections are equivalent to tables
in SQL databases, but they do not enforce a schema allowing for flexibility
in the structure of the documents they contain. For example, you might
have a users collection in a mydatabase database.

5. How Does MongoDB Ensure High Availability and Scalability?

MongoDB ensures high availability and scalability through its features


like replica sets and sharding. Replica sets provide redundancy and
failover capabilities by ensuring that data is always available.

Sharding distributes data across multiple servers, enabling horizontal


scalability to handle large volumes of data and high traffic loads.

6. Explain the Concept of Replica Sets in MongoDB.

A replica set in MongoDB is a group of mongod instances that maintain


the same data set. A replica set consists of a primary node and
multiple secondary nodes.
The primary node receives all write operations while secondary nodes
replicate the primary's data and can serve read operations. If the primary
node fails, an automatic election process selects a new primary to
maintain high availability.

7. What are the Advantages of Using MongoDB Over Other


Databases?

 Flexibility: MongoDB's document-oriented model allows for


dynamic schemas.

 Scalability: Built-in sharding enables horizontal scaling.

 High Availability: Replica sets provide redundancy and automatic


failover.

 Performance: Efficient storage and retrieval with BSON format.

 Ease of Use: JSON-like documents make it easier for developers to


interact with data.

8. How to Create a New Database and Collection in MongoDB?

To create a new database and collection in MongoDB, you can use the
mongo shell:

use mydatabase
db.createCollection("mycollection")

This command switches to mydatabase (creating it if it doesn't exist) and


creates a new collection named mycollection.

9. What is Sharding, and How Does It Work in MongoDB?

Sharding is a method for distributing data across multiple servers in


MongoDB. It allows for horizontal scaling by splitting large datasets into
smaller, more manageable pieces called shards.

Each shard is a separate database that holds a portion of the data.


MongoDB automatically balances data and load across shards, ensuring
efficient data distribution and high performance.

10. Explain the Basic Syntax of MongoDB CRUD Operations.

CRUD operations in MongoDB are used to create, read, update, and delete
documents.

 Create: db.collection.insertOne({ name: "Alice", age: 25 })

 Read: db.collection.find({ name: "Alice" })


 Update: db.collection.updateOne({ name: "Alice" }, { $set: {
age: 26 } })

 Delete: db.collection.deleteOne({ name: "Alice" })

11. How to Perform Basic Querying in MongoDB?

Basic querying in MongoDB involves using the find method to retrieve


documents that match certain criteria. For example:

db.collection.find({ age: { $gte: 20 } })

This query retrieves all documents from the collection where the age field
is greater than or equal to 20.

12. What is an Index in MongoDB, and How to Create One?

An index in MongoDB is a data structure that improves the speed of data


retrieval operations on a collection. You can create an index using the
createIndex method.

For example, to create an index on the name field:

db.collection.createIndex({ name: 1 })

13. How Does MongoDB Handle Data Consistency?

MongoDB provides several mechanisms to ensure data consistency:

 Journaling: MongoDB uses write-ahead logging to maintain data


integrity.

 Write Concerns: It specify the level of acknowledgment requested


from MongoDB for write operations (e.g., acknowledgment from
primary only, or acknowledgment from primary and secondaries).

 Replica Sets: Replication ensures data is consistent across multiple


nodes, and read concerns can be configured to ensure data
consistency for read operations.

14. How to Perform Data Import and Export in MongoDB?

To perform data import and export in MongoDB, you can use the
mongoimport and mongoexport tools. These tools allow you to import
data from JSON, CSV or TSV files into MongoDB and export data from
MongoDB collections to JSON or CSV files.

Import Data:

mongoimport --db mydatabase --collection mycollection --file data.json

This command imports data from data.json into the mycollection


collection in the mydatabase database.
Export Data:

mongoexport --db mydatabase --collection mycollection --out data.json

This command exports data from the mycollection collection in the


mydatabase database to data.json.

15. What are MongoDB Aggregation Pipelines and How are They
Used?

The aggregation pipeline is a framework for data aggregation, modeled


on the concept of data processing pipelines. Documents enter a multi-
stage pipeline that transforms the documents into aggregated results.

Each stage performs an operation on the input documents and passes the
results to the next stage.

db.orders.aggregate([
{ $match: { status: "A" } }, // Stage 1: Filter documents by status
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } }, // Stage 2:
Group by customer ID and sum the amount
{ $sort: { total: -1 } } // Stage 3: Sort by total in descending
order
])

In this example:

 Stage 1 ($match) filters documents by status "A".

 Stage 2 ($group) groups documents by customer ID and calculates


the total amount for each group.

 Stage 3 ($sort) sorts the results by total amount in descending


order.

Aggregation pipelines are powerful and flexible, enabling complex data


processing tasks to be executed within MongoDB.

MongoDB Intermediate Interview Questions

MongoDB Intermediate Interview Questions dive into more complex


features of this NoSQL database. These questions might cover how to
design a good schema, use different indexing strategies, and work with
the aggregation framework.

16. Describe the Aggregation Framework in MongoDB

The Aggregation Framework in MongoDB is a powerful tool for performing


data processing and transformation on documents within a collection. It
works by passing documents through a multi-stage pipeline, where each
stage performs a specific operation on the data, such as filtering,
grouping, sorting, reshaping and computing aggregations.

This framework is particularly useful for creating complex data


transformations and analytics directly within the database.

17. How to Perform Aggregation Operations Using MongoDB?

Aggregation operations in MongoDB are performed using the aggregate


method. This method takes an array of pipeline stages, each stage
representing a step in the data processing pipeline. For example, to
calculate the total sales for each product, you might use the following
aggregation pipeline:

db.sales.aggregate([
{ $match: { status: "completed" } }, // Filter completed sales
{ $group: { _id: "$product", totalSales: { $sum: "$amount" } } }, //
Group by product and sum the sales amount
{ $sort: { totalSales: -1 } } // Sort by total sales in descending order
])

18. Explain the Concept of Write Concern and Its Importance in


MongoDB

Write Concern in MongoDB refers to the level of acknowledgment


requested from MongoDB for write operations. It determines how many
nodes must confirm the write operation before it is considered successful.
Write concern levels range from "acknowledged" (default) to
"unacknowledged," "journaled," and various "replica acknowledged"
levels.

The importance of write concern lies in balancing between data durability


and performance. Higher write concern ensures data is safely written to
disk and replicated, but it may impact performance due to the added
latency.

19. What are TTL Indexes, and How are They Used in MongoDB?

TTL (Time To Live) Indexes in MongoDB are special indexes that


automatically remove documents from a collection after a certain period.
They are commonly used for data that needs to expire after a specific
time, such as session information, logs, or temporary data.

To create a TTL index, you can specify the expiration time in seconds:

db.sessions.createIndex({ "createdAt": 1 }, { expireAfterSeconds: 3600 })

This index will remove documents from the sessions collection 1 hour
(3600 seconds) after the createdAt field's value.
20. How to Handle Schema Design and Data Modeling in
MongoDB?

Schema design and data modeling in MongoDB involve defining how


data is organized and stored in a document-oriented database. Unlike SQL
databases, MongoDB offers flexible schema design, which can be both an
advantage and a challenge. Key considerations for schema design include:

 Embedding vs. Referencing: Deciding whether to embed related


data within a single document or use references between
documents.

 Document Structure: Designing documents that align with


application query patterns for efficient read and write operations.

 Indexing: Creating indexes to support query performance.

 Data Duplication: Accepting some level of data duplication to


optimize for read performance.

 Sharding: Designing the schema to support sharding if horizontal


scaling is required.

21. What is GridFS, and When is it Used in MongoDB?

GridFS is a specification for storing and retrieving large files in MongoDB.


It is used when files exceed the BSON-document size limit of 16 MB or
when you need to perform efficient retrieval of specific file sections.

GridFS splits a large file into smaller chunks and stores each chunk as a
separate document within two collections: fs.files and fs.chunks. This
allows for efficient storage and retrieval of large files, such as images,
videos, or large datasets.

22. Explain the Differences Between WiredTiger and MMAPv1


Storage Engines

WiredTiger and MMAPv1 are two different storage engines in MongoDB,


each with distinct characteristics:

1. WiredTiger:

 Concurrency: Provides document-level concurrency, allowing


multiple operations to be performed simultaneously.

 Compression: Supports data compression, reducing storage space


requirements.

 Performance: Generally offers better performance and efficiency


for most workloads.
 Journaling: Uses a write-ahead logging mechanism for better data
integrity.

2. MMAPv1:

 Concurrency: Provides collection-level concurrency, which can limit


performance under heavy write operations.

 No Compression: Does not support data compression.

 Legacy Engine: It is the older storage engine and has been


deprecated in favor of WiredTiger.

 Simplicity: Simple implementation but lacks advanced features


compared to WiredTiger.

23. How to Handle Transactions in MongoDB?

MongoDB supports multi-document ACID transactions by allowing us to


perform a series of read and write operations across multiple documents
and collections in a transaction.

This ensures data consistency and integrity. To use transactions we


typically start a session, begin a transaction, perform the operations and
then commit or abort the transaction.

Example in JavaScript:

const session = client.startSession();

session.startTransaction();

try {
db.collection1.insertOne({ name: "Alice" }, { session });
db.collection2.insertOne({ name: "Bob" }, { session });
session.commitTransaction();
} catch (error) {
session.abortTransaction();
} finally {
session.endSession();
}

24. Describe the MongoDB Compass Tool and Its Functionalities

MongoDB Compass is a graphical user interface (GUI) tool for MongoDB


that provides an easy way to visualize, explore, and manipulate your data.
It offers features such as:

 Schema Visualization: View and analyze your data schema,


including field types and distributions.
 Query Building: Build and execute queries using a visual interface.

 Aggregation Pipeline: Construct and run aggregation pipelines.

 Index Management: Create and manage indexes to optimize


query performance.

 Performance Monitoring: Monitor database performance,


including slow queries and resource utilization.

 Data Validation: Define and enforce schema validation rules to


ensure data integrity.

 Data Import/Export: Easily import and export data between


MongoDB and JSON/CSV files.

25. What is MongoDB Atlas, and How Does it Differ From Self-
Hosted MongoDB?

MongoDB Atlas is a fully managed cloud database service provided by


MongoDB. It offers automated deployment, scaling, and management of
MongoDB clusters across various cloud providers (AWS, Azure, Google
Cloud). Key differences from self-hosted MongoDB include:

 Managed Service: Atlas handles infrastructure management,


backups, monitoring, and upgrades.

 Scalability: Easily scale clusters up or down based on demand.

 Security: Built-in security features such as encryption, access


controls, and compliance certifications.

 Global Distribution: Deploy clusters across multiple regions for


low-latency access and high availability.

 Integrations: Seamless integration with other cloud services and


MongoDB tools.

26. How to Implement Access Control and User Authentication in


MongoDB?

Access control and user authentication in MongoDB are implemented


through a role-based access control (RBAC) system. You create users and
assign roles that define their permissions. To set up access control:

 Enable Authentication: Configure MongoDB to require


authentication by starting the server with --auth or setting
security.authorization to enabled in the configuration file.

 Create Users: Use the db.createUser method to create users with


specific roles.
db.createUser({
user: "admin",
pwd: "password",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
});

 Assign Roles: Assign roles to users that define their permissions,


such as read, write, or admin roles for specific databases or
collections.

27. What are Capped Collections, and When are They Useful?

Capped collections in MongoDB are fixed-size collections that


automatically overwrite the oldest documents when the specified size
limit is reached. They maintain insertion order and are useful for scenarios
where you need to store a fixed amount of recent data, such as logging,
caching, or monitoring data.

Example of creating a capped collection:

db.createCollection("logs", { capped: true, size: 100000 });

28. Explain the Concept of Geospatial Indexes in MongoDB

Geospatial indexes in MongoDB are special indexes that support


querying of geospatial data, such as locations and coordinates. They
enable efficient queries for proximity, intersections, and other spatial
relationships. MongoDB supports two types of geospatial
indexes: 2d for flat geometries and 2dsphere for spherical geometries.

Example of creating a 2dsphere index:

db.places.createIndex({ location: "2dsphere" });

29. How to Handle Backups and Disaster Recovery in MongoDB?

Handling backups and disaster recovery in MongoDB involves regularly


creating backups of your data and having a plan for restoring data in case
of failure. Methods include:

 Mongodump/Mongorestore: Use the mongodump and


mongorestore utilities to create and restore binary backups.

 File System Snapshots: Use file system snapshots to take


consistent backups of the data files.

 Cloud Backups: If using MongoDB Atlas, leverage automated


backups provided by the service.

 Replica Sets: Use replica sets to ensure data redundancy and high
availability. Regularly test the failover and recovery process.
30. Describe the Process of Upgrading MongoDB to a Newer
Version

Upgrading MongoDB to a newer version involves several steps to ensure a


smooth transition:

 Check Compatibility: Review the release notes and compatibility


changes for the new version.

 Backup Data: Create a backup of your data to prevent data loss.

 Upgrade Drivers: Ensure that your application drivers are


compatible with the new MongoDB version.

 Upgrade MongoDB: Follow the official MongoDB upgrade


instructions, which typically involve stopping the server, installing
the new version, and restarting the server.

 Test Application: Thoroughly test your application with the new


MongoDB version to identify any issues.

 Monitor: Monitor the database performance and logs to ensure a


successful upgrade.

31. What are Change Streams in MongoDB, and How are They
Used?

Change Streams in MongoDB allow applications to listen for real-time


changes to data in collections, databases, or entire clusters. They provide
a powerful way to implement event-driven architectures by capturing
insert, update, replace, and delete operations.

To use Change Streams, you typically open a change stream cursor and
process the change events as they occur.

Example:

const changeStream = db.collection('orders').watch();


changeStream.on('change', (change) => {
console.log(change);
});

This example listens for changes in the orders collection and logs the
change events.

32. Explain the Use of Hashed Sharding Keys in MongoDB

Hashed Sharding Keys in MongoDB distribute data across shards using


a hashed value of the shard key field. This approach ensures an even
distribution of data and avoids issues related to data locality or uneven
data distribution that can occur with range-based sharding.
Hashed sharding is useful for fields with monotonically increasing values,
such as timestamps or identifiers.

Example:

db.collection.createIndex({ _id: "hashed" });


sh.shardCollection("mydb.mycollection", { _id: "hashed" });

33. How to Optimize MongoDB Queries for Performance?

Optimizing MongoDB queries involves several strategies:

 Indexes: Create appropriate indexes to support query patterns.

 Query Projections: Use projections to return only necessary fields.

 Index Hinting: Use index hints to force the query optimizer to use
a specific index.

 Query Analysis: Use the explain() method to analyze query


execution plans and identify bottlenecks.

 Aggregation Pipeline: Optimize the aggregation pipeline stages


to minimize data processing and improve efficiency.

34. Describe the Map-Reduce Functionality in MongoDB

Map-Reduce in MongoDB is a data processing paradigm used to


perform complex data aggregation operations. It consists of two phases:
the map phase processes each input document and emits key-value pairs,
and the reduce phase processes all emitted values for each key and
outputs the final result.

Example:

db.collection.mapReduce(
function() { emit(this.category, this.price); },
function(key, values) { return Array.sum(values); },
{ out: "category_totals" }
);

This example calculates the total price for each category in a collection.

35. What is the Role of Journaling in MongoDB, and How Does It


Impact Performance?

Journaling in MongoDB ensures data durability and crash recovery by


recording changes to the data in a journal file before applying them to the
database files. This mechanism allows MongoDB to recover from
unexpected shutdowns or crashes by replaying the journal.
While journaling provides data safety, it can impact performance due to
the additional I/O operations required to write to the journal file.

36. How to Implement Full-Text Search in MongoDB?

Full-Text Search in MongoDB is implemented using text indexes. These


indexes allow you to perform text search queries on string content within
documents.

Example:

db.collection.createIndex({ content: "text" });


db.collection.find({ $text: { $search: "mongodb" } });

In this example, a text index is created on the content field, and a text
search query is performed to find documents containing the word
"mongodb."

37. What are the Considerations for Deploying MongoDB in a


Production Environment?

Considerations for deploying MongoDB in a production environment


include:

 Replication: Set up replica sets for high availability and data


redundancy.

 Sharding: Implement sharding for horizontal scaling and to


distribute the load.

 Backup and Recovery: Establish a robust backup and recovery


strategy.

 Security: Implement authentication, authorization, and encryption.

 Monitoring: Use monitoring tools to track performance and detect


issues.

 Capacity Planning: Plan for adequate storage, memory, and CPU


resources.

 Maintenance: Regularly update MongoDB to the latest stable


version and perform routine maintenance tasks.

38. Explain the Concept of Horizontal Scalability and Its


Implementation in MongoDB

Horizontal Scalability in MongoDB refers to the ability to add more


servers to distribute the load and data. This is achieved through sharding,
where data is partitioned across multiple shards.
Each shard is a replica set that holds a subset of the data. Sharding allows
MongoDB to handle large datasets and high-throughput operations by
distributing the workload.

39. How to Monitor and Troubleshoot Performance Issues in


MongoDB?

Monitoring and troubleshooting performance issues in MongoDB involve:

 Monitoring Tools: Use tools like MongoDB Cloud Manager,


MongoDB Ops Manager, or third-party monitoring solutions.

 Logs: Analyze MongoDB logs for errors and performance metrics.

 Profiling: Enable database profiling to capture detailed information


about operations.

 Explain Plans: Use the explain() method to understand query


execution and identify bottlenecks.

 Index Analysis: Review and optimize indexes based on query


patterns and usage.

 Resource Utilization: Monitor CPU, memory, and disk I/O usage to


identify resource constraints.

40. Describe the Process of Migrating Data from a Relational


Database to MongoDB

Migrating data from a relational database to MongoDB involves several


steps:

 Schema Design: Redesign the relational schema to fit MongoDB's


document-oriented model. Decide on embedding vs. referencing,
and plan for indexes and collections.

 Data Export: Export data from the relational database in a format


suitable for MongoDB (e.g., CSV, JSON).

 Data Transformation: Transform the data to match the MongoDB


schema. This can involve converting data types, restructuring
documents, and handling relationships.

 Data Import: Import the transformed data into MongoDB using


tools like mongoimport or custom scripts.

 Validation: Validate the imported data to ensure consistency and


completeness.

 Application Changes: Update the application code to interact with


MongoDB instead of the relational database.
 Testing: Thoroughly test the application and the database to ensure
everything works as expected.

 Go Live: Deploy the MongoDB database in production and monitor


the transition.

MongoDB Query Based Interview Questions

MongoDB Query-Based Interview Questions focus on your ability to write


and optimize queries to interact with the database effectively.

These questions typically include tasks like retrieving specific data using
filters, sorting and paginating results, and using projection to select fields.

"[
{
""_id"": 1,
""name"": ""John Doe"",
""age"": 28,
""position"": ""Software Engineer"",
""salary"": 80000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2021-01-15"")
},
{
""_id"": 2,
""name"": ""Jane Smith"",
""age"": 34,
""position"": ""Project Manager"",
""salary"": 95000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2019-06-23"")
},
{
""_id"": 3,
""name"": ""Emily Johnson"",
""age"": 41,
""position"": ""CTO"",
""salary"": 150000,
""department"": ""Management"",
""hire_date"": ISODate(""2015-03-12"")
},
{
""_id"": 4,
""name"": ""Michael Brown"",
""age"": 29,
""position"": ""Software Engineer"",
""salary"": 85000,
""department"": ""Engineering"",
""hire_date"": ISODate(""2020-07-30"")
},
{
""_id"": 5,
""name"": ""Sarah Davis"",
""age"": 26,
""position"": ""UI/UX Designer"",
""salary"": 70000,
""department"": ""Design"",
""hire_date"": ISODate(""2022-10-12"")
}
]"

1. Find all Employees Who Work in the "Engineering" Department.

Query:

db.employees.find({ department: "Engineering" })

Output:

[
{
"_id": 1,
"name": "John Doe",
"age": 28,
"position": "Software Engineer",
"salary": 80000,
"department": "Engineering",
"hire_date": ISODate("2021-01-15")
},
{
"_id": 2,
"name": "Jane Smith",
"age": 34,
"position": "Project Manager",
"salary": 95000,
"department": "Engineering",
"hire_date": ISODate("2019-06-23")
},
{
"_id": 4,
"name": "Michael Brown",
"age": 29,
"position": "Software Engineer",
"salary": 85000,
"department": "Engineering",
"hire_date": ISODate("2020-07-30")
}
]

Explanation:

This query finds all employees whose department field is "Engineering".

2. Find the Employee with the Highest Salary.

Query:

db.employees.find().sort({ salary: -1 }).limit(1)

Output:

[
{
"_id": 3,
"name": "Emily Johnson",
"age": 41,
"position": "CTO",
"salary": 150000,
"department": "Management",
"hire_date": ISODate("2015-03-12")
}
]

Explanation:

This query sorts all employees by salary in descending order and retrieves
the top document, which is the employee with the highest salary.

3. Update the Salary of "John Doe" to 90000.

Query:

db.employees.updateOne({ name: "John Doe" }, { $set: { salary:


90000 } })

Output:

{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

Explanation:
This query updates the salary of the employee named "John Doe" to
90000.

4. Count the Number of Employees in Each Department.

Query:

db.employees.aggregate([
{ $group: { _id: "$department", count: { $sum: 1 } } }
])

Output:

[
{ "_id": "Engineering", "count": 3 },
{ "_id": "Management", "count": 1 },
{ "_id": "Design", "count": 1 }
]

Explanation:

This query groups the employees by the department field and counts the
number of employees in each department.

5. Add a New Field Bonus to All Employees in the "Engineering"


Department with a Value of 5000.

Query:

db.employees.updateMany({ department: "Engineering" }, { $set:


{ bonus: 5000 } })

Output:

{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

Explanation:

This query adds a new field bonus with a value of 5000 to all employees in
the "Engineering" department.

6. Retrieve All Documents in the Employees Collection and Sort


Them by the Length of Their Name in Descending Order.

Query:

db.employees.aggregate([
{ $addFields: { nameLength: { $strLenCP: "$name" } } },
{ $sort: { nameLength: -1 } },
{ $project: { nameLength: 0 } }
])
Output:

[
{
"_id": 2,
"name": "Jane Smith",
"age": 34,
"position": "Project Manager",
"salary": 95000,
"department": "Engineering",
"hire_date": ISODate("2019-06-23")
},
{
"_id": 3,
"name": "Emily Johnson",
"age": 41,
"position": "CTO",
"salary": 150000,
"department": "Management",
"hire_date": ISODate("2015-03-12")
},
{
"_id": 1,
"name": "John Doe",
"age": 28,
"position": "Software Engineer",
"salary": 80000,
"department": "Engineering",
"hire_date": ISODate("2021-01-15")
},
{
"_id": 4,
"name": "Michael Brown",
"age": 29,
"position": "Software Engineer",
"salary": 85000,
"department": "Engineering",
"hire_date": ISODate("2020-07-30")
},
{
"_id": 5,
"name": "Sarah Davis",
"age": 26,
"position": "UI/UX Designer",
"salary": 70000,
"department": "Design",
"hire_date": ISODate("2022-10-12")
}
]

Explanation:

This query calculates the length of each employee's name, sorts the
documents by this length in descending order, and removes the
temporary nameLength field from the output.

7. Find the Average Salary of Employees in the "Engineering"


Department.

Query:

db.employees.aggregate([
{ $match: { department: "Engineering" } },
{ $group: { _id: null, averageSalary: { $avg: "$salary" } } }
])

Output:

[
{ "_id": null, "averageSalary": 86666.66666666667 }
]

Explanation:

This query filters employees to those in the "Engineering" department and


calculates the average salary of these employees.

8. Find the Department with the Highest Average Salary.

Query:

db.employees.aggregate([
{ $group: { _id: "$department", averageSalary: { $avg: "$salary" } } },
{ $sort: { averageSalary: -1 } },
{ $limit: 1 }
])

Output:

[
{ "_id": "Management", "averageSalary": 150000 }
]

Explanation:
This query groups employees by department, calculates the average
salary for each department, sorts these averages in descending order, and
retrieves the department with the highest average salary.

9. Find the Total Number of Employees Hired in Each Year.

Query:

db.employees.aggregate([
{ $group: { _id: { $year: "$hire_date" }, totalHired: { $sum: 1 } } }
])

Output:

[
{ "_id": 2015, "totalHired": 1 },
{ "_id": 2019, "totalHired": 1 },
{ "_id": 2020, "totalHired": 1 },
{ "_id": 2021, "totalHired": 1 },
{ "_id": 2022, "totalHired": 1 }
]

Explanation:

This query groups employees by the year they were hired, which is
extracted from the hire_date field, and counts the total number of
employees hired each year.

10. Find the Highest and Lowest Salary in the "Engineering"


Department.

Query:

db.employees.aggregate([
{ $match: { department: "Engineering" } },
{
$group: {
_id: null,
highestSalary: { $max: "$salary" },
lowestSalary: { $min: "$salary" }
}
}
])

Output:

[
{ "_id": null, "highestSalary": 95000, "lowestSalary": 80000 }
]
Explanation:

This query filters employees to those in the "Engineering" department,


then calculates the highest and lowest salary within this group.

You might also like