mongoose に実装された mongodb のテスト スキーマがあります。
var TestSchema = new mongoose.Schema({ exam:[ Exam ] });
var ExamSchema = mongoose.Schema({type:String, questions: [ { question:{ type: ObjectId, ref: 'Question' }, answer:String } ] });
var QuestionSchema = mongoose.Schema({ desciption:String, solution:String });
テストの考え方は、学生がいくつかの試験のテストに参加する可能性があるということです。各試験には、タイプ名 ( Math または Physics の可能性があります) と、問題の ObjectID のリスト、および学生が記入した対応する回答があります。
このコードは、テスト TestModel.update({'_id':pid,'exam.type':type},{'$push':{'exam.$.questions' :{'question':questionsId,'answer':answer}}},options,function(err,ref){ if(err) { console.log('add question to Exam'.red,err); callback(err , null); }else{ console.log('add question to Exam'.green+ref);
callback(null,ref); } }) 追加するとうまくいきますが、質問と回答を削除することになります。動作しません。
Model.update({'_id':pid,'exam.type':type},{'$pull':{'exam.$.questions':questionId}},options,function(err,ref)
Model.update({'_id':pid,'exam.type':type},{'$pull':{'exam.$.questions.question':questionId}},options,function(err,ref)
Model.update({'_id':pid,'exam.type':type,'exam.questions.question':questionId},{'$pull':{'exam.$.questions.$.question':questionId}},options,function(err,ref)
Model.update({'_id':pid,'exam.type':type,'exam.questions.question':questionId},{'$pull':{'exam.questions.$.question':questionId}},options,function(err,ref)
これらの方法を試しましたが、どれも機能しません