【发布时间】:2013-09-03 15:08:22
【问题描述】:
我有一个用 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}}} ,选项,功能(错误,参考){
如果(错误){
console.log('将问题添加到考试'.red,err);
回调(错误,空);
}别的{
console.log('向考试添加问题'.green+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)
我尝试了这些方法,但这些方法都不起作用
【问题讨论】: