次のモンゴーズモデルで更新する必要があります。モンゴースモデル内の配列を更新する
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 'は'サブ文書'として '応答'を持たないので –
あなたのスキーマに 'reflections'がありますか? –
申し訳ありません@シャイアブロイ、私は質問の間違いを編集しました。それは問題のタイプミスでした。 – rookie