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

$pull(更新演算子)

$pull

$pull演算子は、指定された条件に一致する値のすべてのインスタンスを既存の配列から削除します。

次の環境でホストされる配置には $pull を使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです
  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

$pull 演算子は次の形式をとります。

{ $pull: { <field1>: <value|condition>, <field2>: <value|condition>, ... } }

<field> を埋め込みドキュメントまたは配列で指定するには、ドット表記を使用します。

MongoDB 5.0 以降、更新演算子では名前が文字列ベースのドキュメントフィールドを辞書順に処理します。数値名のフィールドは、数値順に処理されます。詳細については、「更新演算子の動作」を参照してください。

<condition> を指定し、かつ配列要素が埋め込みドキュメントである場合、$pull 演算子は配列要素をコレクション内のドキュメントとみなして <condition> を適用する。例については、「ドキュメントの配列から項目を除く」を参照してください。

削除対象に指定された <value> が配列の場合、$pull は順序を含めて指定された <value> と完全に一致する配列内の要素のみを削除します。

削除対象として指定された <value> がドキュメントの場合、$pull は配列内で完全に一致するフィールドと値を持つ要素のみを削除します。フィールドの順序は一致しない可能性があります。

MongoDB 5.0 以降、$pullなどの更新演算子を空のオペランド式({ })と併用しても、mongod でエラーが発生しなくなりました。空の更新を使用すると変更は一切されず、oplog エントリも作成されません(操作は実行されません)。

stores コレクションを次のように作成します。

db.stores.insertMany( [ { _id: 1, fruits: [ "apples", "pears", "oranges", "grapes", "bananas" ], vegetables: [ "carrots", "celery", "squash", "carrots" ] }, { _id: 2, fruits: [ "plums", "kiwis", "oranges", "bananas", "apples" ], vegetables: [ "broccoli", "zucchini", "carrots", "onions" ] }] )

次の操作は以下の要素を削除します。

  • "apples" と fruits を "oranges" 配列から

  • "carrots" を vegetables 配列から

db.stores.updateMany( { }, { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } })

db.collection.find() で結果を確認します。

{ _id: 1, fruits: [ 'pears', 'grapes', 'bananas' ], vegetables: [ 'celery', 'squash' ]},{ _id: 2, fruits: [ 'plums', 'kiwis', 'bananas' ], vegetables: [ 'broccoli', 'zucchini', 'onions' ]}

profiles コレクションを次のように作成します。

db.profiles.insertOne( { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } )

次の操作により、votes 配列から、($gte)6 以上のすべての項目が削除されます。

db.profiles.updateOne( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

更新操作後、ドキュメントの値は 6 未満のみになります。

{ _id: 1, votes: [ 3, 5 ] }

次の db.collection.bulkWrite() 操作によって以下が実行されます。

  • profilesBulkWrite コレクションを作成する

  • votes 配列から($gte)6 以上のすべての項目を削除する

  • votes 配列から($lte)3 以下のすべての項目を削除する

try { db.profilesBulkWrite.bulkWrite( [ { insertOne: { "document": { _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] } } }, { updateOne: { "filter": { _id: 1 }, "update": { $pull: { votes: { $gte: 6 } } } } }, { updateOne: { "filter": { _id: 1 }, "update": { $pull: { votes: { $lte: 3 } } } } } ] );} catch (e) { print(e);}

操作の実行後、ドキュメントに 6 未満かつ 3 超かの値のみが含まれていることを確認できます。

db.profilesBulkWrite.find()

この操作では、以下を返します。

[ { _id: 1, votes: [ 5 ] } ]

survey コレクションを次のように作成します。

db.survey.insertMany([ { _id: 1, results: [ { item: "A", score: 5 }, { item: "B", score: 8 } ] }, { _id: 2, results: [ { item: "C", score: 8 }, { item: "B", score: 4 } ] }] )

次の操作は、8 に等しい score フィールドと "B" に等しい item フィールドの両方を含むすべての要素を results 配列から削除します。

db.survey.updateMany( { }, { $pull: { results: { score: 8 , item: "B" } } })

$pull 式は、トップレベルのドキュメントとみなして results 配列の各要素に条件を適用します。

操作後、results 配列には 8 と等しい score フィールドと "B" に等しい item フィールドの両方を含むドキュメントは含まれません。

{ _id: 1, results: [ { item: 'A', score: 5 } ] },{ _id: 2, results: [ { item: 'C', score: 8 }, { item: 'B', score: 4 } ]}

$pull 演算子は各要素を最上位オブジェクトとして扱い、クエリを各要素に適用します。一致条件を指定するために、$elemMatch を式で使用する必要はありません。

ただし、次の操作では、元のコレクションからの要素は一切$pullされません。

db.survey.updateMany( { }, { $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } })

注意

以下の方法で survey コレクションを削除します。

db.survey.drop()

次に、この例を実行するために再作成します。

ネストされた配列に埋め込まれたドキュメントを含む新しい survey コレクションを作成します。

db.survey.drop() db.survey.insertMany( [ { _id: 1, results: [ { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] }, { item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] } ] }, { _id: 2, results: [ { item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] }, { item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] } ] }] )

$elemMatch を使用して、answers 配列の要素に複数の条件を指定します。

db.survey.updateMany( { }, { $pull: { results: { answers: { $elemMatch: { q: 2, a: { $gte: 8 } } } } } })

この操作により、一致した各ドキュメントの results 配列が更新されました。埋め込まれた answers 配列の要素が強調表示された行の選択条件と一致したときに、db.collection.updateMany() が results からドキュメントを削除しました。

{ _id: 1, results: [ { item: 'A', score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] } ]},{ _id: 2, results: [ { item: 'C', score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] } ]}
このページを評価