$pull Operator in MongoDB: Quick Guide

In this tutorial for beginners, I am going to discuss the $pull operator in MongoDB.

With MongoDB, you can choose from a variety of operators that are suitable for a wide range of uses. Since MongoDB provides easy-to-understand, extremely sophisticated, and flexible operators, MongoDB-backed applications can do much more than just CRUD. This is undoubtedly another factor that contributes to MongoDB’s popularity.

By using operators, MongoDB is now able to handle a wide range of queries, printing, and updating tasks. It appeals to novices and seasoned developers alike, regardless of their experience level.

Today, I will discuss the $pull operator of MongoDB since it is one of the most commonly used and quite useful operators provided by this database management system.

The $pull operator is one that can be used on an array. It simply removes all instances of a value or values from a given array, that match a given condition. When the value specified to remove is an array, the $pull operator removes all the elements that the value, inside that array. It also follows the same order.

If in case the specified value is a document, then the $pull operator removes only those elements in the array, whose fields and values match. This operator can be used with findAndUpdate() and update methods.

This guide will demonstrate examples and teach you how you can use the $operator in MongoDB. So, let us get started.

The $pull Operator Syntax

Let us see how the $pull operator syntax looks like:

{ $pull: { field1: value|condition, field2: value|condition, ... } }

Using the $pull Operator in MongoDB

Let us get started with using the $pull operator. We will explore different ways we can put this operator to use.

Removing All Matched Items from All Documents Using $pull Operator

Using the $pull operator to remove all items from all documents inside a collection, whose values match the given value.

  • Start the MongoDB service server
  • Choose the database you want to work and move into it:
show dbs
use apparelStore
  • Next, first see what documents we have in our outfits collection:
> db.outfits.find( { } )

{
   _id: 1000,
   tops: [ "tank", "crop", "cami", "maxi", "tube" ],
   pants: [ "sweatpants", "harem", "bell bottoms", "culottes" ]
}
{
   _id: 2000,
   tops: [ "tank", "turtle neck", "cami", "blouse", "sweater" ],
   pants: [ "jeans", "harem", "bell bottoms", "chinos" ]
}
{
   _id: 3000,
   tops: [ "peplum", "crop", "cami", "maxi", "off-shoulder" ],
   pants: [ "cargo", "harem", "joggers", "chinos" ]
}
{
   _id: 4000,
   tops: [ "batwing", "peplum", "boxy", "maxi", "tube" ],
   pants: [ "palazzo", "track pants", "bell bottoms", "culottes" ]
}
{
   _id: 5000,
   tops: [ "tshirt", "crop", "v-neck", "turtle neck", "tube" ],
   pants: [ "sweatpants", "chinos", "jodhpurs", "corduroy trousers" ]
}
  • Now, let’s say we are not selling chinos, Cami, and bell bottoms anymore in any of our stores. This is where we will need to use the $pull operator to remove all the items that match our given value from all our documents.
> db.outfits.updateMany(
    { },
    { $pull: { tops: "cami", pants: { $in: [ "chinos", "bell bottoms" ] } },
    { multi: true }
)

This code should now remove “cami” from the “tops” field and “chinos” and “bell bottoms” from the “pants” fields.

  • Let us take a look at our documents now:
> db.outfits.find( { } )

{
   _id: 1000,
   tops: [ "tank", "crop", "maxi", "tube" ],
   pants: [ "sweatpants", "harem", "culottes" ]
}
{
   _id: 2000,
   tops: [ "tank", "turtle neck", "blouse", "sweater" ],
   pants: [ "jeans", "harem" ]
}
{
   _id: 3000,
   tops: [ "peplum", "crop", "maxi", "off-shoulder" ],
   pants: [ "cargo", "harem", "joggers" ]
}
{
   _id: 4000,
   tops: [ "batwing", "peplum", "boxy", "maxi", "tube" ],
   pants: [ "palazzo", "track pants", "culottes" ]
}
{
   _id: 5000,
   tops: [ "tshirt", "crop", "v-neck", "turtle neck", "tube" ],
   pants: [ "sweatpants", "jodhpurs", "corduroy trousers" ]
}

Removing Matched Items from Array of Documents Using $pull Operator

Let us now learn how to remove matched elements from an array of documents. Let us grab the same example as MongoDB. We will be using the survey collection:

{
   _id: 1,
   results: [
      { item: "A", score: 5 },
      { item: "B", score: 8, comment: "Strongly agree" }
   ]
}
{
   _id: 2,
   results: [
      { item: "C", score: 8, comment: "Strongly agree" },
      { item: "B", score: 4 }
   ]
}
  • Removing all elements from the results array, that contain both a score field equal to 8 and an item field equal to “B”:
db.survey.update(
  { },
  { $pull: { results: { score: 8 , item: "B" } } },
  { multi: true }
)
  • Taking a look at what the documents look like:
{
   "_id" : 1,
   "results" : [ { "item" : "A", "score" : 5 } ]
}
{
  "_id" : 2,
  "results" : [
      { "item" : "C", "score" : 8, "comment" : "Strongly agree" },
      { "item" : "B", "score" : 4 }
   ]
}

The results array does not contain a score field with an “8” value and an item field with a value “B”.

This way we have successfully learned how to use the $pull operator in MongoDB.

Read More: Power Guide to MongoDB updateMany Using Mongoose

Conclusion

Learn to use the $pull operator in MongoDB and explore different ways you can put this operator to use.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 174