$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行为

Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为

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>完全匹配的元素,包括顺序。

Starting in MongoDB 5.0, mongod no longer raises an error when you use an update operator like $pullAll with an empty operand expression ( { } ). 从MongoDB 5.0开始,当使用带有空操作数表达式({ })的更新运算符(如$pullAll)时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。

Examples示例

Create the survey collection:创建survey集合:

db.survey.insertOne( { _id: 1, scores: [ 0, 2, 5, 5, 1, 0 ] } )

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

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

After the update, the scores field no longer has any instances of "0" or "5".更新后,scores字段不再有任何“0”或“5”的实例。

{ "_id" : 1, "scores" : [ 2, 1 ] }
←  $push$each →