On this page本页内容
$pull
¶The $pull
operator removes from an existing array all instances of a value or values that match a specified condition.$pull
运算符从现有数组中删除与指定条件匹配的一个或多个值的所有实例。
The $pull
operator has the form:$pull
运算符的形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
If you specify a 如果指定一个<condition>
and the array elements are embedded documents, $pull
operator applies the <condition>
as if each array element were a document in a collection. <condition>
,并且数组元素是嵌入的文档,$pull
运算符应用<condition>
,就像每个数组元素是集合中的文档一样。See Remove Items from an Array of Documents for an example.有关示例,请参阅从文档数组中删除项。
If the specified 如果指定要删除的<value>
to remove is an array, $pull
removes only the elements in the array that match the specified <value>
exactly, including order.<value>
是数组,$pull
只删除数组中与指定<value>
完全匹配的元素,包括顺序。
If the specified 如果指定要删除的<value>
to remove is a document, $pull
removes only the elements in the array that have the exact same fields and values. <value>
是文档,$pull
只删除数组中具有完全相同字段和值的元素。The ordering of the fields can differ.字段的顺序可能会有所不同。
Given the following document in the 鉴于stores
collection:stores
集合中的以下文档:
The following operation updates all documents in the collection to remove 以下操作将更新集合中的所有文档,以从数组"apples"
and "oranges"
from the array fruits
and remove "carrots"
from the array vegetables
:fruits
中移除"apples"
和"oranges"
,并从数组vegetables
中移除"carrots"
:
After the operation, the 操作完成后,fruits
array no longer contains any "apples"
or "oranges"
values, and the vegetables
array no longer contains any "carrots"
values:fruits
数组不再包含任何"apples"
或"oranges"
值,vegetables
数组不再包含任何"carrots"
值:
$pull
Condition$pull
条件的项目¶Given the following document in the 在profiles
collection:profiles
集合中提供以下文档:
The following operation will remove all items from the 以下操作将从投票数组中删除大于或等于(votes
array that are greater than or equal to ($gte
) 6
:$gte
)6
的所有项目:
After the update operation, the document only has values less than 6:更新操作后,文档的值仅小于6:
A survey
collection contains the following documents:survey
集合包含以下文档:
The following operation will remove from the 以下操作将从results
array all elements that contain both a score
field equal to 8
and an item
field equal to "B"
:results
数组中删除包含等于8
的score
字段和等于"B"
的item
字段的所有元素:
The $pull
expression applies the condition to each element of the results
array as though it were a top-level document.$pull
表达式将条件应用于results
数组的每个元素,就像它是顶级文档一样。
After the operation, the 操作完成后,results
array contains no documents that contain both a score
field equal to 8
and an item
field equal to "B"
.results
数组不包含同时包含等于8
的score
字段和等于"B"
的item
字段的文档。
Because 由于$pull
operator applies its query to each element as though it were a top-level object, the expression did not require the use of $elemMatch
to specify the condition of a score
field equal to 8
and item
field equal to "B"
. $pull
运算符将其查询应用于每个元素,就像它是顶级对象一样,因此表达式不需要使用$elemMatch
来指定score
字段等于8
、item
字段等于"B"
的条件。In fact, the following operation will not pull any element from the original collection.实际上,以下操作不会从原始集合中提取任何元素。
However, if the 但是,如果调查集合包含以下文档,其中survey
collection contained the following documents, where the results
array contains embedded documents that also contain arrays:results
数组包含也包含数组的嵌入文档:
Then you can specify multiple conditions on the elements of the 然后可以使用answers
array with $elemMatch
:$elemMatch
在answers
数组的元素上指定多个条件:
The operation removed from the 该操作从results
array those embedded documents with an answers
field that contained at least one element with q
equal to 2
and a
greater than or equal to 8
:results
数组中删除了包含answers
字段的嵌入文档,该字段至少包含一个q
等于2
且a
大于或等于8
的元素:
See also参阅