Below is a complete Employee (emp) practical example with data and common find() queries, including AND, OR, NOT, and NOR.

1. Create Database and Employee Collection

use company

Insert employee records:

db.emp.insertMany([
  {
    empno: 101,
    ename: "Amit",
    job: "Manager",
    dept: "IT",
    salary: 55000,
    age: 35,
    city: "Rajkot"
  },
  {
    empno: 102,
    ename: "Rahul",
    job: "Developer",
    dept: "IT",
    salary: 45000,
    age: 28,
    city: "Ahmedabad"
  },
  {
    empno: 103,
    ename: "Priya",
    job: "Developer",
    dept: "HR",
    salary: 40000,
    age: 26,
    city: "Rajkot"
  },
  {
    empno: 104,
    ename: "Neha",
    job: "Accountant",
    dept: "Finance",
    salary: 50000,
    age: 32,
    city: "Surat"
  },
  {
    empno: 105,
    ename: "Vijay",
    job: "Manager",
    dept: "Sales",
    salary: 60000,
    age: 40,
    city: "Ahmedabad"
  },
  {
    empno: 106,
    ename: "Kiran",
    job: "Developer",
    dept: "IT",
    salary: 48000,
    age: 30,
    city: "Rajkot"
  },
  {
    empno: 107,
    ename: "Pooja",
    job: "HR Executive",
    dept: "HR",
    salary: 35000,
    age: 25,
    city: "Surat"
  },
  {
    empno: 108,
    ename: "Jay",
    job: "Clerk",
    dept: "Finance",
    salary: 30000,
    age: 24,
    city: "Rajkot"
  }
])

2. Display All Employees

db.emp.find()

Answer

Displays all 8 employee documents.

For a cleaner output:

db.emp.find().pretty()

3. Find One Employee

db.emp.findOne({ empno: 101 })

Answer

Amit
Manager
IT
55000
Rajkot

4. Find Employees from IT Department

db.emp.find({ dept: "IT" })

Answer

Amit
Rahul
Kiran

5. Find Employees with Salary Greater Than 45000

Use $gt:

db.emp.find({
  salary: { $gt: 45000 }
})

Answer

Amit
Neha
Vijay
Kiran

6. Salary Greater Than or Equal to 50000

Use $gte:

db.emp.find({
  salary: { $gte: 50000 }
})

Answer

Amit
Neha
Vijay

7. Salary Less Than 40000

Use $lt:

db.emp.find({
  salary: { $lt: 40000 }
})

Answer

Pooja
Jay

8. Salary Less Than or Equal to 40000

Use $lte:

db.emp.find({
  salary: { $lte: 40000 }
})

Answer

Priya
Pooja
Jay

9. Salary Equal to 50000

db.emp.find({
  salary: 50000
})

Answer

Neha

10. Salary Not Equal to 50000

Use $ne:

db.emp.find({
  salary: { $ne: 50000 }
})

Answer

All employees except Neha.


11. Age Between 25 and 35

db.emp.find({
  age: {
    $gte: 25,
    $lte: 35
  }
})

Answer

Amit
Rahul
Priya
Neha
Kiran
Pooja

12. AND Operator — $and

Find employees who are in IT AND salary > 45000:

db.emp.find({
  $and: [
    { dept: "IT" },
    { salary: { $gt: 45000 } }
  ]
})

Answer

Kiran

Note: Amit is also in IT, but his salary is 55000, so actually the answer is Amit and Kiran.


13. OR Operator — $or

Find employees who work in IT OR HR:

db.emp.find({
  $or: [
    { dept: "IT" },
    { dept: "HR" }
  ]
})

Answer

Amit
Rahul
Priya
Kiran
Pooja

14. OR — Salary 30000 OR 60000

db.emp.find({
  $or: [
    { salary: 30000 },
    { salary: 60000 }
  ]
})

Answer

Vijay
Jay

15. NOT Operator — $not

Find employees whose salary is NOT greater than 45000:

db.emp.find({
  salary: {
    $not: { $gt: 45000 }
  }
})

Answer

Rahul
Priya
Pooja
Jay

16. NOT Equal — $ne

Find employees who are not in IT:

db.emp.find({
  dept: { $ne: "IT" }
})

Answer

Priya
Neha
Vijay
Pooja
Jay

17. NOR Operator — $nor

Find employees who are neither from IT nor HR:

db.emp.find({
  $nor: [
    { dept: "IT" },
    { dept: "HR" }
  ]
})

Answer

Neha
Vijay
Jay

18. Find Rajkot Employees

db.emp.find({
  city: "Rajkot"
})

Answer

Amit
Priya
Kiran
Jay

19. Find Managers

db.emp.find({
  job: "Manager"
})

Answer

Amit
Vijay

20. Find Developers

db.emp.find({
  job: "Developer"
})

Answer

Rahul
Priya
Kiran

21. AND + Comparison Operator

Find employees from IT department AND salary >= 45000:

db.emp.find({
  dept: "IT",
  salary: { $gte: 45000 }
})

Answer

Amit
Rahul
Kiran

22. OR + Comparison Operator

Find employees whose salary is less than 35000 OR greater than 55000:

db.emp.find({
  $or: [
    { salary: { $lt: 35000 } },
    { salary: { $gt: 55000 } }
  ]
})

Answer

Vijay
Jay

23. AND + OR

Find employees who are:

IT AND salary > 40000
OR
HR AND salary > 30000

db.emp.find({
  $or: [
    {
      dept: "IT",
      salary: { $gt: 40000 }
    },
    {
      dept: "HR",
      salary: { $gt: 30000 }
    }
  ]
})

Answer

Amit
Rahul
Priya
Kiran
Pooja

24. Find Employees from Rajkot OR Surat

db.emp.find({
  $or: [
    { city: "Rajkot" },
    { city: "Surat" }
  ]
})

Answer

Amit
Priya
Neha
Kiran
Pooja
Jay

25. Find Employees NOT from Rajkot

db.emp.find({
  city: { $ne: "Rajkot" }
})

Answer

Rahul
Neha
Vijay
Pooja

26. Projection — Display Only Name and Salary

db.emp.find(
  {},
  {
    _id: 0,
    ename: 1,
    salary: 1
  }
)

Answer

Amit   55000
Rahul  45000
Priya  40000
Neha   50000
Vijay  60000
Kiran  48000
Pooja  35000
Jay    30000

27. Sort by Salary Ascending

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

Order

Jay
Pooja
Priya
Rahul
Kiran
Neha
Amit
Vijay

28. Sort by Salary Descending

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

Order

Vijay
Amit
Neha
Kiran
Rahul
Priya
Pooja
Jay

29. Count Employees

db.emp.countDocuments()

Answer

8

30. Count IT Employees

db.emp.countDocuments({
  dept: "IT"
})

Answer

3

Important MongoDB Operators

OperatorMeaning
$eqEqual
$neNot Equal
$gtGreater Than
$gteGreater Than or Equal
$ltLess Than
$lteLess Than or Equal
$andAND
$orOR
$notNOT
$norNOR
$inMatch any value
$ninDoes not match any value

Very important for practical/exam

// AND
db.emp.find({$and:[{dept:"IT"},{salary:{$gt:40000}}]})

// OR
db.emp.find({$or:[{dept:"IT"},{dept:"HR"}]})

// NOT
db.emp.find({salary:{$not:{$gt:45000}}})

// NOR
db.emp.find({$nor:[{dept:"IT"},{dept:"HR"}]})

These four are the main logical operators used with find() in MongoDB.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *