0% found this document useful (0 votes)
26 views8 pages

Mongodb Ex

Uploaded by

Sireesha RM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views8 pages

Mongodb Ex

Uploaded by

Sireesha RM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

21ZC38 - DATA INTENSIVE COMPUTING SYSTEMS

MONGODB EXERCISES
NAME :Tharani S
Roll No : 23MZ05
Exercises in MongoDB. Create a data base in MongoDB for storing patient
and doctor details. Insert patient details and doctor details. Establish
connection between doctor and patient. Modify doctor details for a patient.
Add 2 /more doctors for a patient named XXX. Identify count of patients
under a doctor. If patints count > 4, allot a new dotor to the patient. Allot
doctor to patient based on specialisation. If patients to a doctor becomes 0
generate an alert message. If a doctor leaves a hospital, then delete doctor
from database, allot a new doctor based on speciality to his / her patients.
1. Create patient and doctor collections and inserted 1 record
usemydb;
db.createCollection("Patients");
show collections;
db.Patients.insert({name: 'Tharani',age:25,bloodgroup:'O+ve',docid:'11'});
db.Patients.find().pretty()
db.Doctors.insert({_id:11,name: 'Shanmugam',Spec:'Neurology'});
db.Doctors.find().pretty()
Output:
mycompiler_mongodb> switched to dbmydb
mydb> { ok: 1 }
mydb> Patients
mydb>DeprecationWarning: Collection.insert() is deprecated. Use insertOne,
insertMany, or bulkWrite.
{
acknowledged: true,
insertedIds: { '0': ObjectId('6604e8c01bef4d9307f1c2b3') }
}
mydb> [
{
_id: ObjectId('6604e8c01bef4d9307f1c2b3'),
name: 'Tharani',
age: 25,
bloodgroup: 'O+ve',
docid: '11'
}
]
mydb> { acknowledged: true, insertedIds: { '0': 11 } }
mydb> [ { _id: 11, name: 'Shanmugam', Spec: 'Neurology' } ]
mydb>

[Execution complete with exit code 0]

2. Insert Multiple records


db.Patients.insert([{name: 'Sireesha',age:35,bloodgroup:'O-ve',docid:'12'},
{name: 'Vaishnavi',age:28,bloodgroup:'A+ve',docid:['10','11']},{name:
Anne',age:25,bloodgroup:'B+ve',docid:['11','12']}]);
db.Doctors.insert([{_id:12,name: 'Akash',Spec:'Nephrology'},{_id:10,name:
'Sonu',Spec:'Cardiology'},{_id:13,name: 'Sunny',Spec:'Neurology'}]);
db.Patients.find().pretty()
db.Doctors.find().pretty()

Output:
mydb> [
{
_id: ObjectId('6604eb264d840ce5d077e35d'),
name: 'Tharani',
age: 25,
bloodgroup: 'O+ve',
docid: '11'
},
{
_id: ObjectId('6604eb264d840ce5d077e35e'),
name: 'Sireesha',
age: 35,
bloodgroup: 'O-ve',
docid: '12'
},
{
_id: ObjectId('6604eb264d840ce5d077e35f'),
name: 'Vaishnavi',
age: 28,
bloodgroup: 'A+ve',
docid: [ '10', '11' ]
},
{
_id: ObjectId('6604eb264d840ce5d077e360'),
name: 'Anne',
age: 25,
bloodgroup: 'B+ve',
docid: [ '11', '12' ]
}
]
mydb> [
{ _id: 11, name: 'Shanmugam', Spec: 'Neurology' },
{ _id: 12, name: 'Akash', Spec: 'Nephrology' },
{ _id: 10, name: 'Sonu', Spec: 'Cardiology' },
{ _id: 13, name: 'Sunny', Spec: 'Neurology' }
]
mydb>

3. Update doctor details of a patient


db.Patients.find().pretty()
db.Patients.updateOne({name: 'Sireesha'},{$set:{docid:'13'}});
db.Patients.find().pretty()

Output:
mydb> [
{
_id: ObjectId('6604ecc728684d388b5d3db4'),
name: 'Tharani',
age: 25,
bloodgroup: 'O+ve',
docid: '11'
},
{
_id: ObjectId('6604ecc728684d388b5d3db5'),
name: 'Sireesha',
age: 35,
bloodgroup: 'O-ve',
docid: '12'
},
{
_id: ObjectId('6604ecc728684d388b5d3db6'),
name: 'Vaishnavi',
age: 28,
bloodgroup: 'A+ve',
docid: [ '10', '11' ]
},
{
_id: ObjectId('6604ecc728684d388b5d3db7'),
name: 'Anne',
age: 25,
bloodgroup: 'B+ve',
docid: [ '11', '12' ]
}
]
]
}
mydb> [
{
_id: ObjectId('6604ecc728684d388b5d3db4'),
name: 'Tharani',
age: 25,
bloodgroup: 'O+ve',
docid: '11'
},
{
_id: ObjectId('6604ecc728684d388b5d3db5'),
name: 'Sireesha',
age: 35,
bloodgroup: 'O-ve',
docid: '13'
},
{
_id: ObjectId('6604ecc728684d388b5d3db6'),
name: 'Vaishnavi',
age: 28,
bloodgroup: 'A+ve',
docid: [ '10', '11' ]
},
{
_id: ObjectId('6604ecc728684d388b5d3db7'),
name: 'Anne',
age: 25,
bloodgroup: 'B+ve',
docid: [ '11', '12' ]
}
]

4. Count number of patients under each doctor

db.Patients.aggregate( [
// First Stage
{
$unwind: { path: "$docid", preserveNullAndEmptyArrays: true }
},
// Second Stage
{
$group:
{
_id: "$docid",
totalpatients: { $sum: 1} } }] )
Output:
mydb> ... ... ... ... ... ... ... ... ... ... ... ... ... [
{ _id: '11', totalpatients: 3 },
{ _id: '12', totalpatients: 1 },
{ _id: '13', totalpatients: 1 },
{ _id: '10', totalpatients: 1 }
]

5. Replace doctor id for a patient

db.Patients.updateMany( { name: "Tharani" }, { $set: { docid: [11,12] } });


db.Patients.find().pretty();
Output:
mydb> [
{
_id: ObjectId('660ab22dc33c21d7341c6145'),
name: 'Tharani',
age: 25,
bloodgroup: 'O+ve',
docid: [ 11, 12 ]
},
{
_id: ObjectId('660ab22dc33c21d7341c6146'),
name: 'Sireesha',
age: 35,
bloodgroup: 'O-ve',
docid: '13'
},
{
_id: ObjectId('660ab22dc33c21d7341c6147'),
name: 'Vaishnavi',
age: 28,
bloodgroup: 'A+ve',
docid: [ '10', '11' ]
},
{
_id: ObjectId('660ab22dc33c21d7341c6148'),
name: 'Anne',
age: 25,
bloodgroup: 'B+ve',
docid: [ '11', '12' ]
}
]

Final Code
// Create collections for patients and doctors
db.createCollection("patients");
db.createCollection("doctors");
// Insert patient details
db.patients.insertMany([
{ _id: 1, name: "Tharani", age: 25, gender: "Male", doctor_id: 11 },
{ _id: 2, name: "Shanmugam", age: 28, gender: "Male", doctor_id: 12 },
{ _id: 3, name: "Suman", age: 45, gender: "Male", doctor_id: 11 },
{ _id: 4, name: "Rani", age: 32, gender: "Female", doctor_id: 13 }
]);
// Insert doctor details
db.doctors.insertMany([
{ _id: 11, name: "Dr. Anne", specialization: "Cardiologist", patients: [1, 3] },
{ _id: 12, name: "Dr. Harini", specialization: "Pediatrician", patients: [2] },
{ _id: 13, name: "Dr. Mrudhhula", specialization: "Dermatologist", patients:
[4] }
]);
db.doctors.find().pretty();
db.patients.find().pretty();
// Function to count patients under a doctor
functioncountPatientsUnderDoctor(doctorId) {
returndb.patients.find({ doctor_id: doctorId }).count();
}

// Function to find a doctor based on specialization with fewer than 4 patients


functionfindDoctorWithSpace(specialization) {
returndb.doctors.findOne({ specialization: specialization, patients: { $size: { $lt:
4 } } });
}

// Function to alert if a doctor has 0 patients


functionalertDoctorWithNoPatients(doctorId) {
if (countPatientsUnderDoctor(doctorId) === 0) {
print("Alert: Doctor with ID " + doctorId + " has 0 patients.");
}
}

// Modify doctor details for a patient


db.patients.update({ name: "Tharani" }, { $set: { doctor_id: 13 } });
db.patients.find().pretty();
// Add 2 or more doctors for a patient named "Tharani"
varpatientJohn = db.patients.findOne({ name: "Tharani" });
db.doctors.update({ _id: { $in: [14, 15] } }, { $push: { patients:
patientTharani._id } }, { multi: true });

// Identify count of patients under each doctor and allot a new doctor if count >
4
vardoctorsCursor = db.doctors.find();
while (doctorsCursor.hasNext()) {
var doctor = doctorsCursor.next();
varpatientsCount = countPatientsUnderDoctor(doctor._id);
if (patientsCount> 4) {
varnewDoctor = findDoctorWithSpace(doctor.specialization);
if (newDoctor) {
db.patients.updateMany({ doctor_id: doctor._id }, { $set: { doctor_id:
newDoctor._id } });
print("Patients of Doctor " + doctor.name + " reassigned to Doctor " +
newDoctor.name);
}
}
}

// Alert if any doctor has 0 patients


varallDoctors = db.doctors.find();
while (allDoctors.hasNext()) {
var doc = allDoctors.next();
alertDoctorWithNoPatients(doc._id);
}

// If a doctor leaves a hospital, delete doctor from database and allot a new
doctor based on specialty to his/her patients
db.doctors.remove({ name: "Dr. Anne" });
varpatientsOfRemovedDoctor = db.patients.find({ doctor_id: 11 });
while (patientsOfRemovedDoctor.hasNext()) {
var patient = patientsOfRemovedDoctor.next();
varnewDoctor = findDoctorWithSpace("Cardiologist");
if (newDoctor) {
db.patients.update({ _id: patient._id }, { $set: { doctor_id: newDoctor._id } });
print("Patient " + patient.name + " reallocated to Doctor " + newDoctor.name);
}
}

You might also like