다음 몽구스 모델에 대한 업데이트가 필요합니다.몽구스 모델 내부의 배열 업데이트
var room_schema = new Schema({
title: { type: String, required: true },
questions: [{ //array of reflections
q_index: {type: Number},
q_text: { type: String},
responses: [{ //array of
student: { type: Schema.Types.ObjectId, ref: 'User' },
response: { type: String }
}]
}]
});
module.exports = mongoose.model('Room', room_schema);
필수 값은 내가 방을 업데이트 할 지금
x = {
room: ObjectId("586a0aa0232a3918c8b7f5c9"),
student: ObjectId("5863918c85c9ba0aa0232a7f"),
question: 0,
msg: "Some Message"
}
으로 객체에 있습니다. 내가 반환되는이
Room.update(
{_id:x.room,
'questions.q_index':x.question,
'questions.responses.student':x.student},
{$set:{
'responses.$.student.response' : x.msg
}},function(err, data){
if(err){throw err}
console.log(data);
}
);
msg가 같은 일을 시도하는 것은 { ok: 0, n: 0, nModified: 0 }
및 업데이트가 발생하지 않습니다 말할 필요도 없다. 또한 Room
에 응답 배열이 없을 수도 있습니다. 그런 경우라면 어레이를 생성하고 업데이트해야합니다. 몇 가지 지침을주십시오.
귀하의'응답. $. student.response' ==> '응답에 관한 다음과 같이해야하기 때문에,이 "_id"와 같은 인용 부호로 _id를 넣어보십시오. $. response' 'student '가'sub document'로 'response'를 가지고 있지 않기 때문에 –
스키마에'reflections'이 있습니까? –
죄송합니다 @ShaishabRoy, 질문에서 실수를 수정했습니다. 그것은 질문의 오타였습니다. – rookie