0

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)

これらの方法を試しましたが、どれも機能しません

4

2 に答える 2

0

$次の修飾子で演算子 を使用するには:

{'$pull': {'exam.$.questions': questionId}

$elemMatch:最初にクエリで演算子を使用する必要があります。

{'_id': pid, exam: { $elemMatch: {type: type} } }
于 2016-06-23T07:26:58.083 に答える
-2

他の誰かが提供する可能性のあるmongo構文の回答があります。

私が気に入っている meteor の 1 つの側面は、どこでも javascript/coffeescript を使用できることです。その戦略を mongo 更新の使用に拡張することを謙虚に提案します。別の構文を学ぶのではなく、json/オブジェクト操作機能を直接使用してすべてを $set していることに気づきました。効果があることが証明されるまで取得するフィールドを制限するのは時期尚早の最適化だと言う人もいるでしょう。

于 2013-08-30T20:18:45.617 に答える