AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$pullAll(更新演算子)

$pullAll

The $pullAll operator removes all instances of the specified values from an existing array. Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.

The $pullAll operator 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 ] }
このページを評価

項目一覧