0% found this document useful (0 votes)
9 views

Assignment 3

DBMS practical

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment 3

DBMS practical

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Group A

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:

Develop a MongoDB-based system to manage student records in a university database. The


system should be capable of inserting, reading, updating, and deleting student data using
MongoDB queries. You are also required to perform queries using logical operators for filtering
records based on specific criteria.

Hardware Requirements:

1. A computer with the following minimum specifications:


o Processor: Intel Core i3 or equivalent
o RAM: 4GB or more
o Storage: 50GB free space
o Network adapter (for internet connectivity)

Software Requirements:

1. Operating System: Windows 10 or higher / Linux / macOS


2. Database Software: MongoDB (version 4.x or above)

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.

 Syntax: db.collection.save({ _id: ObjectId("123"), name: "John", age: 21


})

Logical Operators:

Logical operators are used to combine or filter data based on certain conditions.

 $and: Matches documents that satisfy both conditions.


o Example: db.students.find({ $and: [{ age: { $gte: 18 } }, {
course: "Engineering" }] })
 $or: Matches documents that satisfy either condition.
o Example: db.students.find({ $or: [{ age: { $lt: 18 } }, { course:
"Engineering" }] })
 $not: Matches documents that don’t match the condition.
o Example: db.students.find({ age: { $not: { $gt: 25 } } })
Examples:

1. Create a Collection and Insert Documents (Create Operation):

Let's assume we need to store the following details for students: name, age, course, marks, and
address.

Command to insert a single document:

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"
}
})

Command to insert multiple documents:

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"
}
}
])

2. Retrieve Documents (Read Operation):

Query to retrieve all documents in the collection:

db.students.find({})

Query to retrieve students from Pune:

db.students.find({
"address.city": "Pune"
})

Query to retrieve students who scored more than 80 in science:

db.students.find({
"marks.science": { $gt: 80 }
})

3. Update Documents (Update Operation):

Update the age of a student named "Alice Johnson":

db.students.updateOne(
{ "name": "Alice Johnson" },
{ $set: { "age": 23 } }
)

Update marks for all students in "Mechanical Engineering":

db.students.updateMany(
{ "course": "Mechanical Engineering" },
{ $set: { "marks.math": 80 } }
)

4. Delete Documents (Delete Operation):

Delete a student named "Bob Smith":

db.students.deleteOne({ "name": "Bob Smith" })

Delete all students from the city of Nashik:

db.students.deleteMany({ "address.city": "Nashik" })


5. SAVE Method:

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 }
})

 If the _id already exists, this will update the document.


 If the _id does not exist, it will insert a new document.

6. Using Logical Operators:

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" }
]}
]
})

Retrieve students who are not from "Pune":

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" }
]
})

You might also like