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参阅