MongoDB $pop Operator (original) (raw)

Last Updated : 15 Apr, 2026

The $pop operator in MongoDB removes either the first or the last element from an array field during updates, making it useful for maintaining fixed-size arrays such as rolling logs or recent activity lists.

Syntax

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

Examples of $pop in MongoDB

In the following examples, we are working with:

**Database: GeeksforGeeks
**Collection: logs
**Document: Four documents that contain log-related data.

Screenshot-2026-02-13-164927

Example 1: Removing first item from the array

Remove the first element of the recentActions array for the document where user is "Liam" by setting $pop to -1.

db.logs.updateOne(
{ user: "Liam" },
{ $pop: { recentActions: -1 } }
)

**Output:

Screenshot-2026-02-13-165621

Example 2: Removing last item from the array

Remove the last element of the recentActions array for the document where user is "Emma" by setting $pop to 1.

db.logs.updateOne(
{ user: "Emma" },
{ $pop: { recentActions: 1 } }
)

**Output:

Screenshot-2026-02-13-165751

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

Remove the first element of the activity.history array in the nested document for the user "Olivia" by setting $pop to -1.

db.logs.updateOne(
{ user: "Olivia" },
{ $pop: { "activity.history": -1 } }
)

**Output:

Screenshot-2026-02-13-170259