定義
$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.The
$pullAlloperator has the form:{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } } <field>を埋め込みドキュメントまたは配列で指定するには、ドット表記を使用します。
動作
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.
MongoDB5.0 以降、mongod $pullAllなどの更新演算子を空のオペランド式()と併用しても、{ } でエラーが発生しなくなりました。空の更新を使用すると変更は一切されず、 oplogエントリも作成されません(操作は実行されません)。
例
survey コレクションを次のように作成します。
db.survey.insertOne( { _id: 1, scores: [ 0, 2, 5, 5, 1, 0 ] } )
次の操作は、scores 配列から値「0」と「5」のすべてのインスタンスを削除します。
db.survey.updateOne( { _id: 1 }, { $pullAll: { scores: [ 0, 5 ] } } )
更新後、scores フィールドで「0」または「5」のインスタンスがなくなります。
{ "_id" : 1, "scores" : [ 2, 1 ] }