Home > Mobile >  Storing multiple entries for 1 person in Mongodb
Storing multiple entries for 1 person in Mongodb

Time:09-05

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.

CodePudding user response:

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.

  • Related