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
を使用する必要が
あなたはこれを達成するために、まだ何かをしようとしたことがありますか? – Shrabanee
私はすでに私が質問 – Kim
@Shrabanee更新 – Kim