Use MongoDB $Pull to Delete Documents Within an Array

  1. MongoDB $pull Operator
  2. MongoDB $pull Command With Condition
  3. Use $pull in the Nested Array

$pull operator can fetch any existing item from the array. You can pull one or more elements from the array.

You can delete any specific object(s) from the nested array.

MongoDB $pull Operator

$pull is used in MongoDB to remove existing elements from the array. The $pull is used with the update query.

Here’s an example. We inserted the following.

db.cart.insertMany( [
   {
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk", "paper"],
      electronics:["earphone", "bluetooth", "mouse", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water", "paper"],
      electronics:["pendrive", "motherboard", "mouse", "SSD"]
   }
] )

Here are two shopping carts. Let’s say the store has no stock for paper, SSD, or mouse.

So, you want to remove these products from the shopping cart. Then you can use the following query using the $pull operator.

db.cart.updateMany({},
    {
        $pull:{ grocery: "paper",  electronics:{ $in:[ "SSD","mouse" ] } }
    }
)

The $in operator takes an array to match each element with the document. After this operation, the collection will be like the following.

{
      _id: 1,
      grocery:["biscuits", "oil", "honey", "milk"],
      electronics:["earphone", "bluetooth", "keyboard"]
   },
   {
      _id: 2,
      grocery:["biscuits", "oil", "soda", "water"],
      electronics:["pendrive", "motherboard"]
   }

MongoDB $pull Command With Condition

Let’s assume we have the following document.

{
    _id: 1,
    price: [ 31, 50, 55, 66, 98, 25 ]
},
{
    _id: 2,
    price: [ 51, 55, 65, 91, 19, 26 ]
},

Now, you want to remove the elements in the price array with a value greater than 50. Then the command will be like the following.

db.products.updateMany(
    {},
    { $pull: { price:{ $gte : 50} } }
)

You will see that we used {} as the first parameter of the updateMany. This is because we want to update all elements.

Let’s say you want to update only some specific _id, then the command will be like the following.

db.products.updateMany(
    {_id: 1},
    { $pull: { price:{ $gte : 50} } }
)

You can also use the $in operator in the _id to select multiple _id to update and use the pull operation.

Use $pull in the Nested Array

Let’s say you have the following collection of documents.

db.survey.drop()

db.survey.insertMany( [
   {
      _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 } ]
         }
      ]
   }
] )

We want to remove those items from the results array where q equals 2, and a is greater than equals 8. Then the query will be:

db.survey.updateMany(
  { },
  {
     $pull:
        {
           results:
              {
                 answers: { $elemMatch: { q: 2, a: { $gte: 8 } } }
              }
        }
  }
)

After this operation, the updated collection will look like this:

{
  _id: 1,
  results: [
    {
      item: 'A',
      score: 5,
      answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ]
    }
  ]
},
{
  _id: 2,
  results: [
    {
      item: 'C',
      score: 8,
      answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ]
    }
  ]
}

This nested array example was taken from the official documentation of MongoDB. Here you can read the full documentation and know more about the $pull operator.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.