0% found this document useful (0 votes)
3 views15 pages

Week5 BDA

This document outlines a lab experiment for data analysis using MongoDB, covering installation, data import, querying, aggregation, and visualization techniques. It provides step-by-step instructions for setting up MongoDB, importing a sample dataset, and performing various queries and aggregations using MongoDB Compass. The conclusion emphasizes the importance of these skills for effective data analysis.

Uploaded by

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

Week5 BDA

This document outlines a lab experiment for data analysis using MongoDB, covering installation, data import, querying, aggregation, and visualization techniques. It provides step-by-step instructions for setting up MongoDB, importing a sample dataset, and performing various queries and aggregations using MongoDB Compass. The conclusion emphasizes the importance of these skills for effective data analysis.

Uploaded by

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

5.

MongoDB Data Analysis:


Objective

The objective of this lab experiment is to perform data analysis using MongoDB, a NoSQL
database, focusing on data querying, aggregation, and visualization techniques.

Prerequisites

1. MongoDB Installation: Ensure you have MongoDB installed on your machine. You
can download it from MongoDB Download Center.
2. MongoDB Compass: For GUI-based data visualization, install MongoDB Compass
from MongoDB Compass Download.
3. Sample Data: We will use a sample dataset for analysis. For this example, you can
use the Sample Data Sets provided by MongoDB.

Steps

Step 1: Setting Up MongoDB

1. Start MongoDB Service:


o If you’re using Windows, run mongod in the Command Prompt.
o For macOS/Linux, run mongod in the terminal.
2. Connect to MongoDB: Open another terminal and run mongo to access the MongoDB shell.
Run the following command:-
1. sudo systemctl start mongod
2. sudo systemctl enable mongod
3. sudo systemctl status mongod
//The status should be active
//Else follow the steps in case of error:
1. sudo journalctl -u mongod.service --since "10 minutes ago"
2. cat /var/log/mongodb/mongod.log
3. sudo cat /var/log/mongodb/mongod.log
4. vbit@vbit-labs:~$ sudo systemctl stop mongod
5. (base) vbit@vbit-labs:~$ sudo rm /tmp/mongodb-27017.sock
6. (base) vbit@vbit-labs:~$ sudo chown mongodb:mongodb /tmp
7. sudo chmod 1777 /tmp
8. (base) vbit@vbit-labs:~$ sudo systemctl start mongod
9. (base) vbit@vbit-labs:~$ sudo systemctl status mongod
Step 2: Import Sample Data

1. Download Sample Data: Use the following command to import a sample dataset (e.g.,
iris.csv).
2. Import the iris.csv in Mongodb compass.

Step 3: Explore the Data


Here are the steps to import sample data into MongoDB Compass:

Step 1: Open MongoDB Compass

1. Launch MongoDB Compass on your system.


2. Connect to your MongoDB instance by clicking on "New Connection" and
entering your MongoDB connection string (e.g., mongodb://localhost:27017 for
local installations).

Step 2: Create or Select a Database

1. Click on "Create Database" and name it (e.g., sampleDB).


2. Inside the database, create a collection (e.g., analytics), or it will be
automatically created when data is imported.

Step 3: Import Sample Data

1. Click on your database (sampleDB) in the left panel.


2. Select the collection (analytics) or create a new one.
3. Click on "Import Data" in the top right corner.
4. Choose a JSON or CSV file to import.
5. Click "Import" and wait for the process to complete.

Step 4: Explore the Data

• Navigate to your database and collection.


• Click on "Find" to view the imported documents.

Step 5: Perform Queries

You can use MongoDB Compass’s query interface to run basic queries like:

• Find all documents: {} (empty query)


• Find active status documents: { "status": "active" }
Step 6: Aggregation

You can use the Aggregation Tab in Compass to perform operations like:

• Counting documents: { $count: "totalDocuments" }

Grouping & Summing:


Here’s how to perform all the steps in MongoDB Compass clearly:

Step 1: Open MongoDB Compass


1. Launch MongoDB Compass and connect to your database using the connection
string (e.g., mongodb://localhost:27017).
2. If connected successfully, you’ll see a list of databases on the left.

Step 2: Explore the Data

1. Show Databases

• In Compass, you automatically see all available databases in the left-side


panel.
• Equivalent to show dbs; in the shell.

2. Select a Database

• Click on the sampleDB (or any database you imported).


• Equivalent to use sampleDB; in the shell.

3. Show Collections

• Inside sampleDB, you’ll see a list of collections.


• Equivalent to show collections; in the shell.

4. View Sample Data

• Click on the analytics collection.


• Use the "Documents" tab to browse data.
• To limit results (like db.analytics.find().limit(5).pretty();):
• Click on "FILTER" (Query Bar).
• Enter {} (empty query) and set the Limit to 5, then click "Apply".
Step 3: Basic Queries

1. Retrieve All Documents

• In the Filter box, enter {} and click Apply.


• Equivalent to db.analytics.find();.

2. Find Documents Based on a Condition

• To find active users, enter { "status": "active" } in the Filter box.


• Equivalent to db.analytics.find({ "status": "active" });.

Step 4: Aggregation

Go to the Aggregation tab in Compass.

Click "Add Stage" and select $count/$group

1. Counts the total number of records in the iris collection.

"total_records"

o/p:-

total_records

150

2. Finds the maximum and minimum petal length for each species.

_id: "$species",

max_petal_length: {
$max: "$petal_length"

},

min_petal_length: {

$min: "$petal_length"

o/p:-

_id: "setosa"

max_petal_length

1.9

min_petal_length

_id

"versicolor"

max_petal_length

5.1

min_petal_length

3
_id

"virginica"

max_petal_length

6.9

min_petal_length

4.5

3. Sorts species by average petal width from highest to lowest.

average_petal_width: -1

o/p:-

_id

"virginica"

average_petal_width

2.026

_id

"versicolor"
average_petal_width

1.3259999999999998

_id

"setosa"

average_petal_width

0.24400000000000002

4. Finds the count of flowers with sepal width > 3.0, grouped by species.

sepal_width: {

$gt: 3.0

o/p:-

_id

67bffceafc9790da8d3b9499

sepal_length

5.1

sepal_width

3.5

petal_length

1.4
petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b949b

sepal_length

4.7

sepal_width

3.2

petal_length

1.3

petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b949c

sepal_length

4.6

sepal_width

3.1

petal_length
1.5

petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b949d

sepal_length

sepal_width

3.6

petal_length

1.4

petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b949e

sepal_length

5.4

sepal_width

3.9
petal_length

1.7

petal_width

0.4

species

"setosa"

_id

67bffceafc9790da8d3b949f

sepal_length

4.6

sepal_width

3.4

petal_length

1.4

petal_width

0.3

species

"setosa"

_id

67bffceafc9790da8d3b94a0

sepal_length
5

sepal_width

3.4

petal_length

1.5

petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b94a2

sepal_length

4.9

sepal_width

3.1

petal_length

1.5

petal_width

0.1

species

"setosa"

_id

67bffceafc9790da8d3b94a3
sepal_length

5.4

sepal_width

3.7

petal_length

1.5

petal_width

0.2

species

"setosa"

_id

67bffceafc9790da8d3b94a4

sepal_length

4.8

sepal_width

3.4

petal_length

1.6

petal_width

0.2

species

"setosa"
5. Groups the documents by species and counts how many belong to each.

_id: "$species",

count: {

$sum: 1

o/p:-

_id

"setosa"

count

50

_id

"versicolor"

count

50

_id

"virginica"

count

50
Step 5: Data Visualization

1. Export Data for Visualization

• Open the "Export Collection" button (top-right of Compass).


• Choose CSV format.
• Click Export.

2. Load into External Tools

• Open Excel, Tableau, or Google Data Studio.


• Import the CSV file to create graphs.

Step 6: Clean Up

1. Drop Collection

• Click on the "analytics" collection.


• Click the "Drop Collection" button.
• Equivalent to db.analytics.drop();.

2. Close MongoDB Compass

• Simply exit the application.

Step 7: Export Data (Optional)

If needed, export your processed data as CSV or JSON from MongoDB Compass for
visualization in tools like Excel or Tableau.

Would you like specific help with a dataset? 🚀

Resources
• MongoDB Documentation
• MongoDB Aggregation Framework
• MongoDB Data Modeling
Conclusion

In this lab experiment, you learned how to set up MongoDB, import data, perform basic queries
and aggregations, and visualize the results. This foundational knowledge is crucial for data analysis
using MongoDB.

You might also like