Open In App

MongoDB - $pop Operator

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The $pop operator in MongoDB is designed for managing array fields by removing either the first or last element of the array. This operator simplifies the process of maintaining array size and managing data in scenarios where a fixed number of elements is required.

In this article, We will learn about the MongoDB $pop Operator in detail by understanding various examples and so on.

MongoDB $pop Operator

  • The $pop operator in MongoDB provides a straightforward way to manage and maintain the size of an array.
  • $pop operator is a simple operation that directly modifies the array in place.
  • When working with fixed-size arrays such as in scenarios involving rolling logs or fixed-length history tracking, $pop allows us to easily remove the oldest or least relevant elements and ensure the array remains within the desired size.
  • MongoDB provides different types of array update operators to update the values of the array fields in the documents and $pop operator is one of them.
  • This operator is used to remove the first or the last item from the array.

Syntax:

{ $pop: { <field>: <-1 | 1>, ... } }

Here, <field> can specify with dot notation in embedded/nested documents or an array.

  • If you pass -1 value in $pop operator, then it will remove the first item from the array.
  • If you pass 1 value in $pop operator, then it will remove the last item from the array.
  • If the <field> is not an array, then this operator will fail.
  • If $pop operator will remove the last item from the specified field, then the field holds an empty array.
  • You can use this operator with methods like update(), findAndModify(), etc., according to our requirement.

Examples of MongoDB $pop

In the following examples, we are working with:

Database: GeeksforGeeks Collection: contributor Document: two documents that contain the details of the contributor in the form of field-value pairs.

Example 1: Removing first item from the array

In this example, we are removing the first element of the language field in the document that matches the specified condition, i.e., name: "Rohit", by setting the value of $pop operator to -1.

db.contributor.update({name: "Rohit"}, {$pop: { language: -1}})

Output:

Example 2: Removing last item from the array

In this example, we are removing the last element of the language field in the document that matches the specified condition, i.e., name: "Sumit", by setting the value of $pop operator to 1.

db.contributor.update({name: "Sumit"}, {$pop: {language: 1}})

Output:

Example 3: Removing first item from the embedded/nested document

In this example, we are removing the last element of the personal.semesterMarks field in the nested document by setting the value of $pop operator to -1.

db.contributor.update({name: "Sumit"}, 
{$pop: {"personal.semesterMarks": -1}})

Output:

Using-a-negative-index-to-add-items-to-the-array-(1)

Conclusion

The $pop operator effectively aids in controlling the size and content of arrays within MongoDB documents. Its straightforward approach to removing elements from either end of the array makes it a practical tool for maintaining data integrity and relevance.


Next Article

Similar Reads