Mongodb Ex
Mongodb Ex
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>
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>
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' ]
}
]
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 }
]
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();
}
// 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);
}
}
}
// 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);
}
}