$pullAll
On this page本页内容
Definition定义
$pullAll-
The$pullAlloperator removes all instances of the specified values from an existing array.$pullAll运算符从现有数组中删除指定值的所有实例。Unlike the与通过指定查询删除元素的$pulloperator that removes elements by specifying a query,$pullAllremoves elements that match the listed values.$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. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。具有数字名称的字段按数字顺序处理。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, 从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 ] }