Database Design
MongoDB schema design, collection relationships, and indexing strategy.
The Care Nexus uses MongoDB Atlas as its primary database with Mongoose as the ODM layer. The schema is designed around the core entities of the healthcare domain: users, clinics, doctors, patients, appointments, and prescriptions.
Core Collections
| Collection | Purpose | Key Fields |
|---|---|---|
| users | Authentication identity for all roles | email, password (hashed), role, isVerified, language |
| doctors | Doctor profile linked to user | userId, clinicId, specialization, schedule, licenseNo |
| patients | Patient profile linked to user | userId, dob, bloodGroup, allergies[] |
| clinics | Clinic entity and settings | name, address, phone, doctors[], workingHours |
| appointments | All appointment records across the system | doctorId, patientId, clinicId, dateTime, status |
| prescriptions | Structured prescriptions with medicines | doctorId, patientId, diagnosis, medicines[], audioUrl |
| medicalrecords | Pointer from patient to prescription | patientId, prescriptionId, familyMemberId |
| familymembers | Dependents linked to patient accounts | patientId, name, dob, relationship |
| messages | Encrypted chat messages | conversationId, senderId, content (encrypted) |
| conversations | Chat rooms between doctor-patient pairs | participants[], unreadCount |
| notifications | Persistent notification records | userId, type, message, read |
| revenueentries | Consultation fee records | doctorId, appointmentId, amount, createdAt |
| analytics | Nightly aggregated clinic stats | clinicId, date, totalPatients, totalRevenue |
Key Relationships
User → Doctor / Patient
Each User document represents the authentication identity. Doctor and Patient collections extend it via userId reference. This keeps auth data separate from profile data and simplifies role resolution.
Appointment → Prescription
Each appointment has a 1:1 relationship with prescription via appointmentId. Both entities also store doctorId and patientId for efficient queries without joins.
Indexing Strategy
- Appointments: compound index on
(doctorId, dateTime) - Appointments: compound index on
(patientId, status) - Prescriptions: index on
patientId - Users: unique index on
email - Messages: compound index on
(conversationId, createdAt)
Connection pooling
The Mongoose connection is initialized once at startup with a max pool size of 10 for efficient request handling. This can be tuned for high-traffic deployments.