Database Engineering (Lab)
Lab Exercise 4 - Using Logical Operators to Query
Data in MongoDB Using mongosh Console
Objective
This exercise will help you learn how to use logical operators ($and, $or, $not, $nor) to
query data in MongoDB using the mongosh console.
Objective
1. Insert sample data into a collection.
2. Query data using logical operators.
3. Understand the usage of each logical operator in MongoDB.
Prerequisites
• MongoDB is installed and running.
• Access to the mongosh console.
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)
Database Engineering (Lab)
Steps
1. Launch the mongosh Console
Open your terminal or command prompt and type:
mongosh
2. Set Up a Database and Collection
a) Switch to a database:
use logicalOperatorsDB
b) Insert sample data into a collection:
db.employees.insertMany([
{ name: "Alice", age: 25, department: "Engineering", salary: 60000 },
{ name: "Bob", age: 35, department: "HR", salary: 50000 },
{ name: "Carol", age: 30, department: "Engineering", salary: 70000 },
{ name: "David", age: 40, department: "Sales", salary: 55000 },
{ name: "Eve", age: 28, department: "HR", salary: 62000 }
])
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)
Database Engineering (Lab)
Output:
{
acknowledged: true,
insertedIds: {
"0": ObjectId("64f94a92b1d0c1a1e62f3c83"),
"1": ObjectId("64f94a92b1d0c1a1e62f3c84"),
"2": ObjectId("64f94a92b1d0c1a1e62f3c85"),
"3": ObjectId("64f94a92b1d0c1a1e62f3c86"),
"4": ObjectId("64f94a92b1d0c1a1e62f3c87")
}
}
3. Query Using Logical Operators
a) Using $and
Query: Find employees in the "Engineering" department with a salary greater than
$60,000.
db.employees.find({
$and: [
{ department: "Engineering" },
{ salary: { $gt: 60000 } }
]
})
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)
Database Engineering (Lab)
Output:
[
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c85"), name: "Carol", age: 30, department:
"Engineering", salary: 70000 }
]
b) Using $or
Query: Find employees in either the "HR" department or those older than 30 years.
db.employees.find({
$or: [
{ department: "HR" },
{ age: { $gt: 30 } }
]
})
Output:
[
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c84"), name: "Bob", age: 35, department:
"HR", salary: 50000 },
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c86"), name: "David", age: 40, department:
"Sales", salary: 55000 },
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c87"), name: "Eve", age: 28, department:
"HR", salary: 62000 }
]
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)
Database Engineering (Lab)
c) Using $not
Query: Find employees not in the "Engineering" department.
db.employees.find({
department: { $not: { $eq: "Engineering" } }
})
Output:
[
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c84"), name: "Bob", age: 35, department:
"HR", salary: 50000 },
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c86"), name: "David", age: 40, department:
"Sales", salary: 55000 },
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c87"), name: "Eve", age: 28, department:
"HR", salary: 62000 }
]
d) Using $nor
Query: Find employees who are neither in the "HR" department nor earning more than
$60,000.
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)
Database Engineering (Lab)
db.employees.find({
$nor: [
{ department: "HR" },
{ salary: { $gt: 60000 } }
]
})
Output:
[
{ _id: ObjectId("64f94a92b1d0c1a1e62f3c86"), name: "David", age: 40, department:
"Sales", salary: 55000 }
]
Summary
• Logical Operators Used:
o $and: Matches documents that satisfy all the specified conditions.
o $or: Matches documents that satisfy at least one condition.
o $not: Negates a condition.
o $nor: Matches documents that do not satisfy any of the specified
conditions.
This exercise demonstrates how to query data using logical operators for various
scenarios in MongoDB.
Prepared by: Prof. Hitesh Kumar Sharma (Cluster Head, CSO Cluster)