![]() |
VOOZH | about |
The $pull operator in MongoDB removes matching values from an array field during updates, enabling in-place modification of arrays without rewriting the entire document.
{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }To understand MongoDB $pull Operator we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called contributor.
Remove the skill "Java" from all contributors who have it.
db.contributor.updateMany(
{ skills: "Java" },
{ $pull: { skills: "Java" } }
)
Output:
Remove the skills "JavaScript" and "Python" from all contributors who have them.
db.contributor.updateMany(
{ skills: { $in: ["JavaScript", "Python"] } },
{ $pull: { skills: { $in: ["JavaScript", "Python"] } } }
)
Output:
Remove skills that are shorter than 5 characters from all contributors.
db.contributor.updateMany(
{},
{ $pull: { skills: { $regex: "^.{1,4}$" } } }
)
The regex "^.{1,4}$" matches strings of length 1 to 4, so $pull removes skills shorter than 5 characters.
Output:
Here are some uses of $pull operator: