-1

Here is my User Schema

{  
   "user_collection":[  
      {  
         "_id":"xxx",
         "name":"NAME",
         "prescription":  
            {  
               "doctor_id":  
                  [{  
                     "_id":"xxx",
                     "medicine_id":"MEDICINE_ID",
                  }]
            },
         "meal":{  
               "meal_name":{
               "start_time":"START_TIME",
               "end_time":"END_TIME"
               }  
         },
         "created_at":"CREATED_AT",
         "updted_at":"UPDATED_AT"
      }
   ]
}

Note : _id given just for understanding

I need to insert document inside the prescription array. Here is the condition

If the new doctor_id is given, it should add in the prescription array like

{
    "_id" : ObjectId("5813288eaa0f1231de477d92"),
    "name" : "andrew",
    "prescription" : [ 
        {
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0528"),
                    "medicine_id" : "10011241343"
                }
            ],
            "_id" : ObjectId("58143d484e26a229873b0527"),
            "doctor_id" : "5813221ace684e2b3f5f0a6d"
        }
    ]
}

And if i given the doctor_id that already exists it should add like

{
    "_id" : ObjectId("5813288eaa0f1231de477d92"),
    "name" : "andrew",
    "prescription" : [ 
        {
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0528"),
                    "medicine_id" : "10011241343"
                }
            ],
            "prescription" : [ 
                {
                    "_id" : ObjectId("58143d484e26a229873b0529"),
                    "medicine_id" : "10011241349"
                }
            ],
            "_id" : ObjectId("58143d484e26a229873b0527"),
            "doctor_id" : "5813221ace684e2b3f5f0a6d"
        }
    ]
}

What i have tried is

dbModel.user.update({
                    _id: req.body.user_id
                }, {
                    $set: {
                        prescription:   [ { "doctor_id" : req.body.doctor_id, "prescription" : [
                        {
                            "medicine_id" : req.body.medicine_id

                        }]} ],
                    }
                }, {
                    upsert: true
                }, function(err) {

                    if (err) {
                        res.status(202).json({
                            "success": "0",
                            "message": err
                        })
                    } else {
                        res.status(200).json({
                            "success": "1",
                            "message": "Prescription given successfully"
                        });
                    }
})

I don't know how to check whether the doctor_id already exists and if it does not exists it should add a new array, and if it exists it should add inside the existing arrays

SA__
  • 437
  • 3
  • 7
  • 13

1 Answers1

0

Take a look in this answer.

But basically you can use the $ operator which identifies an element in an array.

You can see here some mongodb array operators.

Community
  • 1
  • 1
lucas.coelho
  • 894
  • 1
  • 9
  • 16