Welcome back to the Community forums ![]()
To remove elements from a collection using the $pull operator, you don’t need to use the $elemMatch operator. This is because $pull treats each element as a top-level object and applies the query to each element individually. Therefore, you can simply specify the match conditions in the expression without using $elemMatch.
If you do use $elemMatch, the $pull operation will not remove any elements from the original collection.
Here’s an updated version of your query without $elemMatch:
db.survey.updateMany(
{},
{ $pull: { results: { score: { $lte: 4 } } } }
)
And it will return the following output:
[
{
_id: 1,
results: [
{ item: "A", score: 5 },
{ item: "B", score: 8 }
]
},
{
_id: 2,
results: [
{ item: "C", score: 8 },
]
}
]
For more information, please refer to the $pull - Remove Items from an Array of Documents documentation.
I hope it helps.
Regards,
Kushagra