I’ve been reading through the documentation on $pull, $elemMatch and arrayFilters but can’t seem to find the right way to pull elements from an array.
Given this initial data:
db.survey.insertMany([
{
_id: 1,
results: [
{ item: "A", score: 5 },
{ item: "B", score: 8 }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8 },
{ item: "B", score: 4 }
]
}
] )
And let’s say we want to pull (remove) elements from the results array in all documents - where the score is less than or equal to 4.
According to the documentation this would seem like the right way:
db.survey.updateMany(
{ },
{ $pull: { results: { $elemMatch: { score: { $lte: 4 } } } } }
)
But this doesn’t do the trick. Result as follows:
{ acknowledged: true,
insertedId: null,
matchedCount: 2,
modifiedCount: 0,
upsertedCount: 0 }
Appreciate any help on the matter.