$pop
On this page
Definition
-
$pop
-
The
$pop
operator removes the first or last element of an array. Pass$pop
a value of-1
to remove the first element of an array and1
to remove the last element in an array.The
$pop
operator has the form:To specify a
<field>
in an embedded document or in an array, use dot notation.
Behavior
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.
Examples
Remove the First Item of an Array
Given the following document in a collection students
:
The following example removes the first element (8
) in the scores
array:
After the operation, the updated document has the first item 8
removed from its scores
array:
Remove the Last Item of an Array
Given the following document in a collection students
:
The following example removes the last element (10
) in the scores
array by specifying 1
in the $pop
expression:
After the operation, the updated document has the last item 10
removed from its scores
array: