$pullAll

On this page本页内容

Definition定义

$pullAll

The $pullAll operator removes all instances of the specified values from an existing array. $pullAll运算符从现有数组中删除指定值的所有实例。Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.$pull运算符通过指定查询来删除元素不同,$pullAll会删除与列出的值匹配的元素。

The $pullAll operator has the form:$pullAll运算符的形式如下:

{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }

To specify a <field> in an embedded document or in an array, use dot notation.若要在嵌入式文档或数组中指定一个<field>,请使用点表示法

Behavior行为

If a <value> to remove is a document or an array, $pullAll removes only the elements in the array that match the specified <value> exactly, including order.如果要删除的<value>是文档或数组,$pullAll只删除数组中与指定<value>完全匹配的元素,包括顺序。

Examples示例

Given the following document in the survey collection:鉴于survey集合中的以下文件:

{ _id: 1, scores: [ 0, 2, 5, 5, 1, 0 ] }

The following operation removes all instances of the value 0 and 5 from the scores array:以下操作将从scores数组中删除值05的所有实例:

db.survey.update( { _id: 1 }, { $pullAll: { scores: [ 0, 5 ] } } )

After the operation, the updated document has all instances of 0 and 5 removed from the scores field:操作完成后,更新的文档将从scores字段中删除05的所有实例:

{ "_id" : 1, "scores" : [ 2, 1 ] }