On this page本页内容
$pop
¶The $pop
operator removes the first or last element of an array. $pop
运算符删除数组的第一个或最后一个元素。Pass 将值$pop
a value of -1
to remove the first element of an array and 1
to remove the last element in an array.-1
传递给$pop
以移除数组中的第一个元素,将值1
传递给$pop
以移除数组中的最后一个元素。
The $pop
operator has the form:$pop
运算符的形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
The 如果$pop
operation fails if the <field>
is not an array.<field>
不是数组,$pop
操作将失败。
If the 如果$pop
operator removes the last item in the <field>
, the <field>
will then hold an empty array.$pop
运算符删除了<field>
中的最后一项,<field>
将保留一个空数组。
Given the following document in a collection 在students
:students
集合中提供以下文件:
The following example removes the first element (以下示例删除了8
) in the scores
array:scores
数组中的第一个元素(8
):
After the operation, the updated document has the first item 操作完成后,更新后的文档将从其8
removed from its scores
array:scores
数组中删除第一项8
:
Given the following document in a collection 在students
:students
集合中提供以下文档:
The following example removes the last element (以下示例通过在10
) in the scores
array by specifying 1
in the $pop
expression:$pop
表达式中指定1
来删除scores
数组中的最后一个元素(10
):
After the operation, the updated document has the last item 操作完成后,更新后的文档将从其10
removed from its scores
array:scores
数组中删除最后一项10
:
See also参阅