Search This Blog

Monday, 2 March 2026

mongo db in ubuntu

 

 Step 1: Update System

sudo apt update
sudo apt upgrade -y

Step 2: Install Required Packages

sudo apt install -y curl gnupg

 Step 3: Import MongoDB Public GPG Key

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor

 Step 4: Add MongoDB Repository

 For Ubuntu 22.04 (Jammy)

echo "deb [ arch=amd64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

👉 For Ubuntu 20.04 (Focal)

echo "deb [ arch=amd64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

 Step 5: Update Package List Again

sudo apt update

 Step 6: Install MongoDB

sudo apt install -y mongodb-org

 Step 7: Start MongoDB Service

sudo systemctl start mongod

Enable auto-start on boot:

sudo systemctl enable mongod

 Step 8: Check MongoDB Status

sudo systemctl status mongod

You should see:

Active: active (running)

 Step 9: Verify MongoDB Shell

mongosh

If successful, you'll see:

Current Mongosh Log ID:
Using MongoDB:

Exit shell:

exit

 Check Installed Version

mongod --version

 Quick Test

Inside mongosh:

show dbs
use testdb
db.users.insertOne({name: "Raghavendra", role: "Data Engineer"})
db.users.find()

If you tell me your Ubuntu version (lsb_release -a), I can give exact commands customized for your setup.

Implement Functions: Count – Sort – Limit – Skip – Aggregate using MongoDB

Here’s a clear practical implementation of:

  • count

  • sort

  • limit

  • skip

  • aggregate

using MongoDB (via mongosh).


🔹 Step 1: Create Sample Collection

Open Mongo shell:

mongosh

Create database and collection:

use companyDB

db.employees.insertMany([
{ name: "Ravi", department: "IT", salary: 60000, age: 28 },
{ name: "Anita", department: "HR", salary: 45000, age: 32 },
{ name: "Kiran", department: "IT", salary: 75000, age: 30 },
{ name: "Meena", department: "Finance", salary: 50000, age: 29 },
{ name: "Arjun", department: "IT", salary: 80000, age: 35 },
{ name: "Divya", department: "HR", salary: 47000, age: 27 }
])

 Count Documents

➤ Count All Documents

db.employees.countDocuments()

➤ Count with Condition

db.employees.countDocuments({ department: "IT" })

 Sort Documents

➤ Sort by Salary (Ascending)

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

➤ Sort by Salary (Descending)

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

 Limit Documents

Return first 3 records:

db.employees.find().limit(3)

 Skip Documents

Skip first 2 records:

db.employees.find().skip(2)

 Combine Sort + Limit + Skip (Pagination Example)

Example: Page 2 (3 records per page)

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

 Aggregate (Advanced Operations)

Aggregation uses pipeline stages.


🔹 A. Group by Department & Count Employees

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

🔹 B. Average Salary per Department

db.employees.aggregate([
{
$group: {
_id: "$department",
avgSalary: { $avg: "$salary" }
}
}
])

🔹 C. Highest Salary per Department

db.employees.aggregate([
{
$group: {
_id: "$department",
maxSalary: { $max: "$salary" }
}
}
])

🔹 D. Filter + Group + Sort (Real Interview Example)

👉 IT employees average salary sorted descending

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

 Real-Time Scenario (Top 2 Highest Paid Employees)

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

No comments:

Post a Comment

Hadoop Analytics

mongo db in ubuntu

   Step 1: Update System sudo apt update sudo apt upgrade -y Step 2: Install Required Packages sudo apt install -y curl gnupg  St...