2016-10-20 9 views
2

mongoDBのネストされたarrray内の要素を削除するのが苦労しています。ネストされた配列内で文書を検索して削除します

var question = new Schema({ 
    problem: { 
    type: String, 
    required: true 
    }, 
    multipleChoices: [{ 
    type: String, 
    required: true 
    }], 
    correctAnswer: { 
    type: Number, 
    required: true 
    } 
}); 

var testCategorySchema = new Schema({ 
    category: { 
    type: String, 
    required: true 
    }, 
    tests: [{ 
    version: { 
     type: String, 
     required: true 
    }, 
    questions: [question] 
    }] 
}); 

たとえば、私はこのようなそのスキーマのインスタンスを持っている:

が、私はこのスキーマを持っていると言うことができますカテゴリで「blablabla」:例えば

{ 
    category: "Math", 
    tests: [ 
    { 
     _id : ObjectId("580859310a0d4b40ac1c6034"), 
     version: "1A", 
     questions: [ 
     { 
      problem: "blablabla", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ], 
      correctAnswer: 1 
     }, 
     { 
      problem: "blibliblibli", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ] 
      correctAnswer: 1 
     } 
     ] 
    }, 
    { 
     _id : ObjectId("580859310a0d4r40ax1c5034"), 
     version: "1B", 
     questions: [ 
     { 
      problem: "auauauauau", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ] 
      correctAnswer: 1 
     }, 
     { 
      problem: "blibliblibli", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ] 
      correctAnswer: 1 
     } 
     ] 
    } 
    ] 
} 

を、どのように私はこの問題を削除することができますmongooseJSを使って数学とバージョン1A?

私はすでにこのコードを使用して数学のカテゴリとバージョン1(a)に問題を削除するには:

TestCategory 
    .findOne(
     { 
     category: req.params.testCategory, 
     'tests.version': req.params.testVersion 
     }, 
     { 
     'tests.version.$': 1 
     } 
    ) 
    .then(function(testVersion) { 
     res.send(testVersion); 
    }) 
    .catch(function(err) { 
     console.log(err); 
     next(err); 
    }); 

たとえば、私は数学のカテゴリとバージョン1Aを検索し、そのコードからの結果は、このようなものです:

_id: "580859190a0d4b40ac1c6033", 
tests: [ 
    { 
     _id : ObjectId("580859310a0d4b40ac1c6034"), 
     version: "1A", 
     questions: [ 
     { 
      problem: "blablabla", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ], 
      correctAnswer: 1 
     }, 
     { 
      problem: "blibliblibli", 
      multiplechoices: [ 
      "bla bla", "blb blb", "blc blc" 
      ] 
      correctAnswer: 1 
     } 
     ] 
    } 
] 

結果はすでにフィルタリングされているため、バージョン1Aのみが表示されます(ただし、categoryIdは引き続き表示されます)。今私は適切かつ良い方法で質問を削除する方法が混乱しています。あなたは正しいテストを更新してから質問削除する$pullを使用するためには、位置演算子$updateを使用する必要が

+1

あなたはこれを達成するために、まだ何かをしようとしたことがありますか? – Shrabanee

+0

私はすでに私が質問 – Kim

+1

@Shrabanee更新 – Kim

答えて

0

TestCategory.update(
    { 
     category: 'Math', 
     'tests.version': '1A' 
    }, 
    { 
     $pull: { 
      'tests.$.questions': { problem: 'blablabla' } 
     } 
    } 
) 
+0

'$プル更新させて、何かを試してみました:{ 'テスト$質問。' を:{問題: 'blablabla'} }' – Kim

+0

どのように私は質問/問題で、それを削除することができますID? – Kim

+0

あなたは 'problem'値で削除するように求めました。あなたのスキーマやサンプルのdocでは問題のIDはないので、どのようにそれらを削除したいのですか?あなたが質問ごとにidを持っていたら、上記のコードで 'problem 'を' _id'に置き換えてください。私はこれを説明する必要はないと思います。あなたはこれらの質問をする前に自分で考えて試してみる必要があります。 – TomG

関連する問題