π₯ Clinic Management System β MongoDB Project
A multi-doctor SaaS platform built on MongoDB Atlas for managing clinic appointments, medical rep visits, and doctor schedules.
This project is a Clinic Management System built using MongoDB Atlas , designed to manage doctors, patients, appointments, medical representatives, and clinic schedules.
The system demonstrates:
β
Data Modeling β Embedded & Referenced relationships
β
Aggregation Framework β 16 real-world queries
β
Business Logic β booking validation, scheduling conflicts
β
Multi-role Authentication β doctor, patient, medical rep, admin
β
SaaS Architecture β multiple doctors on one platform
Feature
Description
π Authentication
Users collection with 4 roles
π¨ββοΈ Doctors
Profile + clinic info + consultation fee
π§βπ€βπ§ Patients
Profile + blood type + medical info
π
Appointments
Booking with day, time, status
π Medical Reps
Company info + drug specialty
π Rep Visits
Full visit lifecycle + embedded summary
π Schedules
Weekly working hours per doctor
π« Clinic Days
Closed dates + exception handling
ποΈ Database Structure
Stores accounts for all system users.
Field
Type
Description
_id
int
Custom ID
name
string
Full name
email
string
Unique email
password
string
Hashed password
role
string
doctor Β· patient Β· medical rep Β· admin
Doctor-specific profile data.
Field
Type
Description
_id
ObjectId
Auto-generated
user_id
int
β users._id
specialization
string
e.g. Cardiology
clinic_name
string
Display name
consultation_fee
int
Fee in EGP
Patient profile and medical info.
Field
Type
Description
_id
ObjectId
Auto-generated
user_id
int
β users._id
age
int
Patient age
gender
string
male / female
phone
string
Contact number
blood_type
string
A+, B-, O+, etc.
Patient bookings linked to doctors.
Field
Type
Description
_id
ObjectId
Auto-generated
doctor_id
ObjectId
β doctors._id
patient_id
ObjectId
β patients._id
day
string
SundayβSaturday
time
string
HH:MM format
status
string
booked / cancelled
Medical representative data.
Field
Type
Description
_id
ObjectId
Auto-generated
user_id
int
β users._id
company_name
string
Pharma company
company_phone
string
Company number
drug_specialty
string
Medical field
phone
string
Rep direct number
Visit requests + embedded visit summary.
Field
Type
Description
_id
ObjectId
Auto-generated
doctor_id
ObjectId
β doctors._id
rep_id
ObjectId
β medical_reps._id
date
ISODate
Visit date
status
string
completed Β· cancelled Β· no-show Β· rescheduled
visit_summary
embedded
samples Β· duration Β· topic Β· feedback
Embedded document example:
"visit_summary" : {
"samples" : 3 ,
"duration_min" : 22 ,
"discussion_topic" : " Cardio drug" ,
"feedback" : " Doctor accepted samples"
}
Weekly working hours per doctor.
Field
Type
Description
_id
ObjectId
Auto-generated
doctor_id
ObjectId
β doctors._id
day
string
Working day
patient_start_time
string
e.g. "09:00"
patient_end_time
string
e.g. "13:00"
rep_start_time
string
e.g. "13:00"
rep_end_time
string
e.g. "14:00"
max_patients
int
Daily patient limit
max_reps
int
Daily rep limit
Handles closed days and exceptions.
Field
Type
Description
_id
ObjectId
Auto-generated
doctor_id
ObjectId
β doctors._id
date
ISODate
Specific date
is_open
boolean
false = closed
reason
string
e.g. "National Holiday"
users
βββ doctors (user_id β users._id)
βββ patients (user_id β users._id)
βββ medical_reps (user_id β users._id)
doctors
βββ appointments (doctor_id β doctors._id)
βββ rep_visits (doctor_id β doctors._id)
βββ Schedules (doctor_id β doctors._id)
βββ clinic_days (doctor_id β doctors._id)
appointments
βββ patients (patient_id β patients._id)
rep_visits
βββ medical_reps (rep_id β medical_reps._id)
Referenced β all cross-collection links use ObjectId or int references.
Embedded β visit_summary lives inside each rep_visits document.
π Aggregation Queries (14)
#
Query
Stages Used
1
Count total doctors per specialization
$group, $sum
2
Combine doctor info with total patients
$lookup, $unwind, $addFields, $size
3
Identify low-utilization doctors
$lookup, $addFields, $match
π§βπ€βπ§ Patients & Appointments
#
Query
Stages Used
4
Count total patients per day
$group, $sum
5
Identify peak day
$group, $sort, $limit
6
Identify peak hour
$group, $sort, $limit
π Medical Reps & Visits
#
Query
Stages Used
7
Total rep visits per day
$group, $dateToString
8
Most active medical rep
$group, $sort, $limit, $lookup
9
Compare patients vs rep visits per doctor
$group, $lookup, $addFields
π Schedules & Clinic Logic
#
Query
Stages Used
10
Weekly schedule for a specific doctor
$match, $lookup
11
Retrieve doctor closed days
$match, $lookup
12
Calculate occupancy rate
$group, $project, $divide, $multiply
13
Detect idle days
$group, $push, $setDifference
14
Detect scheduling conflicts
$match, $addFields, $switch, $dayOfWeek, $lookup
Booking Request
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β 1. Is the clinic open? β clinic_days β
β 2. Does a schedule exist? β Schedules β
β 3. Is time within range? β patient/rep hoursβ
β 4. Is the slot available? β appointments β
β 5. Is max capacity reached? β max_patients β
βββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
β
Booking Confirmed / β Rejected
Collection
Records
users
50 (10 doctors + 10 reps + 30 patients)
doctors
10
patients
30
medical_reps
10
appointments
31
rep_visits
100
Schedules
25
clinic_days
10
Create a free cluster at cloud.mongodb.com
Create database named clinicDB
Database Access β Add DB User β role: readWrite on clinicDB
Network Access β Add IP β 0.0.0.0/0 (development)
Copy your connection string
1. users (doctors) β db.users.insertMany([...])
2. doctors β db.doctors.insertMany([...])
3. users (patients) β db.users.insertMany([...])
4. patients β db.patients.insertMany([...])
5. users (reps) β db.users.insertMany([...])
6. medical_reps β db.medical_reps.insertMany([...])
7. appointments β db.appointments.insertMany([...])
8. rep_visits β db.rep_visits.insertMany([...])
9. Schedules β db.Schedules.insertMany([...])
10. clinic_days β db.clinic_days.insertMany([...])
β οΈ Always insert users before any role-specific collection.
Requirement
Status
How
Secure Authentication
β
users collection β 4 roles
Business Logic Cycle
β
5-step booking validation
Embedded Documents
β
visit_summary in rep_visits
Referenced Relationships
β
user_id, doctor_id, patient_id, rep_id
Aggregation Pipelines
β
14 queries β $group, $lookup, $switch...
DB Users + Permissions
β
Atlas β readWrite on clinicDB
Network Access
β
Atlas β IP Whitelist
MongoDB Atlas Deployment
β
clinicDB on Atlas Cluster
MongoDB Atlas β Cloud database
MongoDB Aggregation Framework β Data analysis
NoSQL Data Modeling β Embedded + Referenced
Efficient use of references for scalability
Separation of base schedules and exceptions
Embedded visit_summary demonstrates real-world nested documents
14 aggregation queries cover the full analytics lifecycle
Ready for Dashboard integration (Power BI / Tableau / Metabase)