定義
$pushThe
$pushoperator appends a specified value to an array.
互換性
次の環境でホストされる配置には $push を使用できます。
- MongoDB Atlas はクラウドでの MongoDB 配置のための完全管理サービスです
MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン
MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン
構文
The $push operator has the form:
{ $push: { <field1>: <value1>, ... } }
<field> を埋め込みドキュメントまたは配列で指定するには、ドット表記を使用します。
動作
MongoDB 5.0 以降、更新演算子では名前が文字列ベースのドキュメントフィールドを辞書順に処理します。数値名のフィールドは、数値順に処理されます。詳細については、「更新演算子の動作」を参照してください。
If the field is absent in the document to update, $push adds the array field with the value as its element.
フィールドが配列でない場合、操作は失敗します。
If the value is an array, $push appends the whole array as a single element. To add each element of the value separately, use the $each modifier with $push. For an example, see Append Multiple Values to an Array. For a list of modifiers available for $push, see Modifiers.
MongoDB5.0 以降、mongod $pushなどの更新演算子を空のオペランド式()と併用しても、{ } でエラーが発生しなくなりました。空の更新を使用すると変更は一切されず、 oplogエントリも作成されません(操作は実行されません)。
Modifiers
You can use the $push operator with the following modifiers:
When used with modifiers, the $push operator has the form:
{ $push: { <field1>: { <modifier1>: <value1>, ... }, ... } }
The processing of the $push operation with modifiers occur in the following order, regardless of the order in which the modifiers appear:
配列を更新して、正しい位置に要素を追加します。
指定されている場合は、ソートします。
指定されている場合は、配列をスライスします。
配列を保存します。
例
このページの例では、sample_mflixサンプルデータセットのデータを使用します。このデータセットを自己管理型MongoDB配置にロードする方法の詳細については、サンプルデータセットをロードする を参照してください。サンプルデータベースに変更を加えた場合、このページの例を実行するには、データベースを削除して再作成する必要がある場合があります。
配列に対する値の追加
The following example uses the $push operator to append "Classic" to the genres array in the matching movie document:
db.movies.updateOne( { title: "The Dark Knight" }, { $push: { genres: "Classic" } } )
この操作では、次の結果を返します。
{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }
複数のドキュメントの配列への値の追加
The following example uses the $push operator to append "Acclaimed" to the genres array in all matching movie documents:
db.movies.updateMany( { "imdb.rating": { $gt: 9 } }, { $push: { genres: "Acclaimed" } } )
この操作では、次の結果を返します。
{ acknowledged: true, insertedId: null, matchedCount: '...', modifiedCount: '...', upsertedCount: 0 }
配列に対する複数の値の追加
Use $push with the $each modifier to append multiple values to the array field.
db.movies.updateOne( { title: "The Dark Knight" }, { $push: { genres: { $each: [ "Modern Classic", "Award-Winning" ] } } } )
この操作では、次の結果を返します。
{ acknowledged: true, insertedId: null, matchedCount: 1, modifiedCount: 1, upsertedCount: 0 }
複数の修飾子を持つ $push 演算子を使用する
次のドキュメントを students コレクションに追加します。
db.students.insertOne( { "_id" : 5, "quizzes" : [ { "wk": 1, "score" : 10 }, { "wk": 2, "score" : 8 }, { "wk": 3, "score" : 5 }, { "wk": 4, "score" : 6 } ] } )
The following $push operation uses:
quizzes配列に複数のドキュメントを追加するための$each修飾子、変更された
quizzes配列のすべての要素をscoreフィールドで降順にソートするための$sort修飾子、quizzes配列の最初の 3 つのソートされた要素のみを保持するための$slice修飾子。
db.students.updateOne( { _id: 5 }, { $push: { quizzes: { $each: [ { wk: 5, score: 8 }, { wk: 6, score: 7 }, { wk: 7, score: 6 } ], $sort: { score: -1 }, $slice: 3 } } } )
操作後、配列には最高得点の 3 つのクイズだけが残ります。
{ "_id" : 5, "quizzes" : [ { "wk" : 1, "score" : 10 }, { "wk" : 2, "score" : 8 }, { "wk" : 5, "score" : 8 } ] }