Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
Update of object in MongoDB with node.js

So I have a db model defined in my server.js file which will be used for a POST :

var department = mongoose.model('department', {
  departmentName: String,
  rooms: [{
    roomNumber: String,
    width: Number,
    height: Number,
    posX: Number,
    posY: Number,
    sanitary: Boolean,
    childcareArea: Boolean,
    lounge: Boolean,
    patient: {
      patientnr: Number,
      firstname: String,
      lastname: String,
      reasonOfHospitalization: String,
      dateOfHospitalization: String,
      expectedDateOfDischarge: String,
      vegetarian: Boolean,
      needsHelp: Boolean,
      comments: String,
      department: String,
      roomNumber: String,
      nextTreatment: {
        type: String,
        shortDescription: String,
        timestamp: String
      }
    }
  }]
});

Now what I want to achieve is that my post call updates the patient object.

 public postPatient(patient: Patient) {
    var headers = new Headers();
    headers.append('Content-Type', 'application/json; charset=utf-/8');

    let url ='http://localhost:8080/api/departments/patients/' + patient.patientnr;

    this.http.post(url, JSON.stringify(patient), headers)
      .map(res => res.json());
  }

This is how I handle my post, but it updates nothing in my database...

app.post('/api/departments/patients/:id', function(req, res) {
  var patient = req.body.patient;

  department.findOneAndUpdate(
    { "rooms.patient.patientnr": parseInt(req.params.id) },
    {
      "rooms": {
        "$elemMatch": {
          "patient.patientnr": parseInt(req.params.id)
        }
      }
    }, {
      "$set": {
        "rooms.patient": patient
      }
    }, {
     new : true
    },
    function (err, dept) {
      if (err){
        console.log(err.stack);
        return res.send(err);
      }
      return res.json({
        data: department,
        status: 'success'
      });
    });
});
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Something like this should work for you. Finds and replace the patient for the selected room.

department.findOneAndUpdate(
    { "rooms.patient.patientnr": parseInt(req.params.id) },
    { "$set": {"rooms.$.patient": patient}},
    {new : true}
    ....
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!