3

Hiii, I have a MEAN stack application that make use of a mongoose model defined as below:

appointment.js

var mongoose = require('mongoose');
module.exports = mongoose.model('Appointment', {
appt_date: String,
details: [ {
            appt_time: String,
            patient_id : String,
            patient_name : String,
            contact_no: String,
            doctor_name : String,
            purpose : String
          }
      ]
});

I want to generate a document like the following:

{
 appt_date: 13/13/2013,
 details: [
    {
        appt_time: 09:00 AM,
        patient_id: 2015/2016,
        patient_name: David John,
        contact_no: 8965741234,
        doctor_name: Albert ,
        purpose: Consultation,

    }
}

I can't generate a document when using the following Express JS code:

var Appointment = require('../../models/appointment');
var appointments = new Appointment ({ "appt_date" : req.body.date});
appointments.save();
Appointment.update({ appt_date: req.body.date},
{ 
  $push: {
           'details':  {    
                         appt_time:"09:00 AM",
                         patient_id:"2015/2016",
                         patient_name:"Wilfred",
                         contact_no:"8965741234",
                         doctor_name: "Albert",
                         purpose: "Consultation"
                       }
          }
 },
   {
      upsert:true
   });

After performing the above operations, the result that I get is as follows:

"_id": ObjectId('57b6eefd2b494e802ba146d8'),
"appt_date": "12/20/2014",
"details": [],
"__v": 0

I am not able to push any data to the "details" array. What I am in need is a single date entry (appt_date), multiple details (details array). Please help to solve this out.

Thank You.

3 Answers 3

1

No need to do an update after save(), just use the push() method on the details key and then save. For example:

var Appointment = require('../../models/appointment');
var appointments = new Appointment ({ "appt_date" : req.body.date});
appointments.details.push({    
    appt_time: "09:00 AM",
    patient_id: "2015/2016",
    patient_name: "Wilfred",
    contact_no: "8965741234",
    doctor_name: "Albert",
    purpose: "Consultation"
});
appointments.save(function(err, appointment){
    if (err) handleErr(err);
    console.log(appointment);
});
Sign up to request clarification or add additional context in comments.

3 Comments

@ Chridam - Thanks .... On execution, this always creates a new document, right ? The values mentioned inside the update command comes in a second step. What I need is - Step 1) Select a date - this should add a document with appt_date = selected date. Step 2) A form will be displayed from where the details like appt_time, patient_id and etc etc comes from. I don't want to create a new document for every entry. One date = one document, multiple details inside the "details" array. How can this be achieved ?
@ Chridam - I need to create a document upon the selection of the date with the defined model, later when the user completes the info, push those info into the "details" array. I am trying to create an appointment scheduler. So when the user selects a "date" and insert some info, and later on when the user selects the "same date" and add some info, those info should be pushed to "details" array..
@ Chridam - What I did was this: On selecting the date, I sent the date back to server and used the code 'var appointments = new Appointment ({ "appt_date" : req.body.date });' to create a document. At the same instance a modal will be opened to where the info is provided. Upon receiving this info, I used the rest of the code 'Appointment.update({appt_date: req.body.date})'....... At this instance, the mentioned code does no work on the created document. How can I het this corrected. Creation & pushing are done at two instances.
0

Try with this code:

var Appointment = require('../../models/appointment');
var appointments = new Appointment();
appointments.appt_date = req.body.date;
appointment.details.push({appt_time:"09:00 AM",patient_id:"2015/2016", patient_name:"Wilfred",contact_no:"8965741234",doctor_name: "Albert",
purpose: "Consultation"});
appointments.save(function(err){
   if(!err)
      console.log("saved")
})

6 Comments

@ Md Nazmul Bilash - Thanks. I need to create the document at an instance with appt_date and later update/push the information to the previously created document, i.e into the details array. i don't want to create a new document every time when a date is selected. Please help to solve this...
First Time to create a document : Appointment ({ "appt_date" : req.body.date}); if you want to push other inf var appointments = newormation later then : Appointent.find({_id : 'Id of appointment'}).exec(function(err,appoinment){ if(appoinment){ appointment.details.push({appt_time:"09:00 AM", patient_id:"2015/2016", patient_name:"Wilfred", contact_no:"8965741234", doctor_name: "Albert", purpose: "Consultation"}); appointments.save(function(err){ if(!err) console.log("saved") }) } })
yeah I did that. Later I tried the following code. Appointment.update({ appt_date: req.body.date},{ $push: { 'details': { appt_time:12, patient_id:1025, patient_name:"roger", contact_no:8089, doctor_name: "jestin", purpose: "RCT" } } },{upsert:true}); This won't push any data to the details array. I even tried _id instead of appt_date to find the exact document, but failed. What is wrong with this code .. ?
I think its not saving your data. Did you called save() after update ??
inside the function to open the modal, i used the following code: var appointments = new Appointment ({ "appt_date" : req.body.date}); later inside another function which pass the information (appt_time, name,id etc), i used the following code: Appointment.update({ _id: req.body.id}, { $push: { 'details': { appt_time:12, patient_id:1025, patient_name:"roger", contact_no:8089, doctor_name: "jestin", purpose: "RCT" } } },{upsert:true}); I have the _id of the previously generated document inside the function. But the operation fails. Where should I correct this code
|
0

You are updating document against the date and might be there are more than 1 app_date, this scenario will update multiple document ., you have to add multi : true object

Appointment.update({ appt_date: req.body.date},
{ 
 $push: {
       'details':  {    
                     appt_time:"09:00 AM",
                     patient_id:"2015/2016",
                     patient_name:"Wilfred",
                     contact_no:"8965741234",
                     doctor_name: "Albert",
                     purpose: "Consultation"
                   }
      }
},
{multi: true},
 {
  upsert:true
 });

or you can update it by _id

1 Comment

@ Muhammad Ali - Thanks. What I need is a single document with a single appt_date and multiple entries for details array. appt_date is generated at an instance and i want to create a document with the mentioned model. at a later instance I need to update this document, i.e push the info to the details array. I don't want to create a new document every time when a date is selected. What I need is single date entry in a document, multiple information entries in details array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.