- Reference >
- Operators >
- Update Operators >
- Array Update Operators >
- $pop
$pop¶
On this page
Definition¶
-
$pop¶ The
$popoperator removes the first or last element of an array. Pass$popa value of-1to remove the first element of an array and1to remove the last element in an array.The
$popoperator has the form:To specify a
<field>in an embedded document or in an array, use dot notation.
Behavior¶
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.
The $pop operation fails if the <field> is not an array.
If the $pop operator removes the last item in the
<field>, the <field> will then hold an empty array.
Starting in MongoDB 5.0, mongod no longer raises an
error when you use an update operator like $pop
with an empty operand expression ( { } ). An empty update results
in no changes and no oplog entry is created (meaning that the
operation is a no-op).
Examples¶
Remove the First Item of an Array¶
Create the students collection:
The following example removes the first element, 8, from the
scores array:
The first element, 8, has been removed from the scores array: