Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Storing multiple entries for 1 person in Mongodb

I have the following schema for my database of patients.

 {
  "_id": {
    "$oid": "6314abbe7b3971e8a0de558f"
  },
  "user": "person X",
  "age": 26,
  "bloodGroup": "O+",
  "height": 180.34,
  "weight": 74,
  "contactNumber": 123456789,
  "symptoms": "headache",
  "prescribedMedicine": "medicine A",
  "diet": "diet A",
  "createdAt": {
    "$date": {
      "$numberLong": "1662299070355"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1662299070355"
    }
  },
  "__v": 0
}

I can create, update and delete the patients in my database. Now, I want to store multiple visits of the same patient in my database. For example, if the same patient visits 3 times, I would want to store his visits in a way that I can get all the visits of the same patient by 1 get call.
I had the idea of making a get call based on a field like name or contact number.

But before proceeding, I wanted to get some suggestion because I am new to using mongodb and I am not sure if this is a good way of approaching this case or some other.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create another collection "visits" and create a document for each visit with the following schema:

{
  _id: "VISIT_ID",
  patientId: "USER_ID", // _id from patients collection
  doctorId: "..."m
  ...otherRequiredFields
}

You can then query all visits of a given user like this:

db.visits.find({ patientId: ObjectId("USER_ID") })

Alternatively you can store all visits in the patient document itself in an array but it might be easier to query and process data with another collection. Also MongoDB documents have a max size of 16 MB.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading