Assignment 3
Assignment 3
Assignment No: 03
Title: MongoDB Queries: Design and Develop MongoDB Queries using CRUD operations (Use
CRUD operations, SAVE method, logical operators, etc.)
Aim:
To design and develop MongoDB queries to perform Create, Read, Update, and Delete (CRUD)
operations, utilizing MongoDB's SAVE method, logical operators, and other features.
Problem Statement:
Hardware Requirements:
Software Requirements:
Theory:
MongoDB is a NoSQL database designed for flexible schema and high scalability. It stores data
in JSON-like BSON format, which supports complex and hierarchical data structures.
CRUD stands for Create, Read, Update, and Delete—these are the four basic operations of
persistent storage. MongoDB provides a flexible querying mechanism, allowing complex filters
and operations using logical operators, comparisons, and more.
CRUD Operations:
1. Create (Insert): This operation adds new records (documents) to the database.
o Syntax: db.collection.insertOne({ ... })
o Example: db.students.insertOne({ name: "John", age: 20, course:
"Engineering" })
2. Read (Query): This operation retrieves records from the database. Various query
operators and filters can be used for more complex searches.
o Syntax: db.collection.find({ ... })
o Example: db.students.find({ course: "Engineering" })
3. Update (Modify): This operation modifies existing records in the database.
o Syntax: db.collection.updateOne({ query }, { $set: { ... } })
o Example: db.students.updateOne({ name: "John" }, { $set: { age: 21
} })
4. Delete (Remove): This operation removes records from the database.
o Syntax: db.collection.deleteOne({ ... })
o Example: db.students.deleteOne({ name: "John" })
SAVE Method:
The save() method in MongoDB inserts a document if it doesn’t exist, or updates the document
if it exists. It has been deprecated in some versions but still relevant in older versions.
Logical Operators:
Logical operators are used to combine or filter data based on certain conditions.
Let's assume we need to store the following details for students: name, age, course, marks, and
address.
db.students.insertOne({
"name": "Alice Johnson",
"age": 22,
"course": "Computer Science",
"marks": {
"math": 85,
"science": 90,
"english": 88
},
"address": {
"city": "Pune",
"state": "Maharashtra",
"country": "India"
}
})
db.students.insertMany([
{
"name": "Bob Smith",
"age": 20,
"course": "Mechanical Engineering",
"marks": {
"math": 70,
"science": 75,
"english": 80
},
"address": {
"city": "Mumbai",
"state": "Maharashtra",
"country": "India"
}
},
{
"name": "Charlie Davis",
"age": 23,
"course": "Civil Engineering",
"marks": {
"math": 65,
"science": 72,
"english": 78
},
"address": {
"city": "Nashik",
"state": "Maharashtra",
"country": "India"
}
}
])
db.students.find({})
db.students.find({
"address.city": "Pune"
})
db.students.find({
"marks.science": { $gt: 80 }
})
db.students.updateOne(
{ "name": "Alice Johnson" },
{ $set: { "age": 23 } }
)
db.students.updateMany(
{ "course": "Mechanical Engineering" },
{ $set: { "marks.math": 80 } }
)
If you want to upsert a student (insert if they don't exist, update if they do), you can use the
save() method (deprecated).
Example:
db.students.save({
"_id": ObjectId("5f4d5b76c9e77c23456"),
"name": "David Green",
"age": 25,
"course": "Electrical Engineering",
"marks": { "math": 75, "science": 78 }
})
Retrieve students who are older than 20 and study "Computer Science" or "Mechanical
Engineering":
db.students.find({
$and: [
{ "age": { $gt: 20 } },
{ $or: [
{ "course": "Computer Science" },
{ "course": "Mechanical Engineering" }
]}
]
})
db.students.find({
"address.city": { $not: { $eq: "Pune" } }
})
Retrieve students who either scored more than 85 in English or are from "Mumbai":
db.students.find({
$or: [
{ "marks.english": { $gt: 85 } },
{ "address.city": "Mumbai" }
]
})