Definition定义
$pullAllThe$pullAlloperator removes all instances of the specified values from an existing array. Unlike the$pulloperator that removes elements by specifying a query,$pullAllremoves elements that match the listed values.$pullAll运算符从现有数组中删除指定值的所有实例。与通过指定查询删除元素的$pull运算符不同,$pullAll删除与列出的值匹配的元素。The$pullAlloperator 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. Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.从MongoDB 5.0开始,更新运算符按字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
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, 从MongoDB 5.0开始,当您使用带有空操作数表达式(mongod no longer raises an error when you use an update operator like $pullAll with an empty operand expression ( { } ). { })的$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 ] }