2017-01-09 9 views
0

다음 몽구스 모델에 대한 업데이트가 필요합니다.몽구스 모델 내부의 배열 업데이트

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에 응답 배열이 없을 수도 있습니다. 그런 경우라면 어레이를 생성하고 업데이트해야합니다. 몇 가지 지침을주십시오.

+0

귀하의'응답. $. student.response' ==> '응답에 관한 다음과 같이해야하기 때문에,이 "_id"와 같은 인용 부호로 _id를 넣어보십시오. $. response' 'student '가'sub document'로 'response'를 가지고 있지 않기 때문에 –

+0

스키마에'reflections'이 있습니까? –

+0

죄송합니다 @ShaishabRoy, 질문에서 실수를 수정했습니다. 그것은 질문의 오타였습니다. – rookie

답변

0

Sry,하지만 필자는 담당자가 충분하지 않아서 의견을 쓸 수 없으므로 답변으로 내 고지문을 작성해야합니다.

방을 _id로 검색 할 때 첫 번째 검색어에 무엇인가가 나타납니다. 그것은이 게시물에

stackoverflow: updating-an-array-inside-a-mongoose-model

+0

이것은 정확하게 동일한 o/p를 제공합니다.'{ok : 0, n : 0, nModified : 0} ' 내가 누락 된 것이있을 수 있습니다. 전체 계약서를 작성해야 할 수도 있습니다. – rookie

+0

내가 보낸 링크를 확인하십시오, MB 거기에 뭔가 재미있을 것입니다. 또한 http://mongoosejs.com/docs/subdocs.html을 확인해야합니다. – JavaEvgen