I am trying to remove values from nested array using mongoose in node js. My schema structure is like this:
([{_id: 1,
results: [
{ item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
{ item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] },
{ item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] }
]
}])
I want to remove values from answers where a is 8. my query is
db.collection.update({"results.item":{$in:["C","B"]}},{$pull:{"results.$.answers":{a:8}}})
This query is working fine but updating only one document. Please help.
my desired output is
([{
_id: 1,
results: [
{ item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
{ item: "B", score: 8, answers: [ { q: 2, a: 9 } ] }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8, answers: [ { q: 2, a: 7 } ] },
{ item: "B", score: 4, answers: [ { q: 1, a: 0 } ] }
]
}])
Thanks in advance.