The Wayback Machine - https://web.archive.org/web/20260228025421/https://www.geeksforgeeks.org/mongodb/mongodb-pull-operator/

MongoDB $pull Operator

Last Updated : 13 Feb, 2026

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.

  • Removes all instances of specified values from an array.
  • Works on arrays inside embedded (nested) documents.
  • Leaves the array unchanged if no matching value is found.
  • Performs safe, in-place updates without errors.

Syntax

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }
  • <field1>, <field2>, ... specify the array fields from which values should be removed.
  • <value | condition> defines the value or matching condition to pull from each array.

Examples of $pull Operator

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.

Screenshot-2026-02-13-154241

Example 1: Removing a Specific value

Remove the skill "Java" from all contributors who have it.

db.contributor.updateMany(
  { skills: "Java" },
  { $pull: { skills: "Java" } }
)

Output:

Screenshot-2026-02-13-154400

Example 2: Removing Multiple Values

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:

Screenshot-2026-02-13-154526

Example 3: Removing Based on Condition

Remove skills that are shorter than 5 characters from all contributors.

db.contributor.updateMany(
  {},
  { $pull: { skills: { $regex: /^.{1,4}$/ } } }
)

Output:

Screenshot-2026-02-13-154631

Usage of $pull Operator

Here are some usage of $pull operator:

  • Remove specific elements from arrays for data cleanup and customization.
  • Apply conditions to match and remove embedded documents within arrays.
  • Remove exact values or documents that match specified fields and values.
  • Use with update methods like updateMany() and findAndModify() as needed.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment

Explore