如何在mongodb中更新数组中的子对象

时间:2017-01-06 17:39:14

标签: angularjs node.js mongodb mongoose

我有以下问题,我编辑患者信息并希望更新此患者信息。这样做的最佳方式是什么?

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
      }
    }
  }]
});

我可能需要这样的东西......

app.post('/api/departments/patients/:id', function(req, res) {
  department.update({..})
});

2 个答案:

答案 0 :(得分:0)

你需要的东西是

{{1}}

答案 1 :(得分:0)

因为我看到你也在使用角度js,所以你可以传递你想要更新的元素。

所以你的req.body可以包含你想要更新的变量。 记住它用于更新对象的$ set和用于更新数组的$ push

现在

{{1}}