On this page本页内容
$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>
,请使用点表示法。
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, 从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条目(这意味着该操作是无操作)。
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 ] }