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

$push(更新演算子)

$push

The $push operator 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エントリも作成されません(操作は実行されません)。

You can use the $push operator with the following modifiers:

Modifier
説明

配列フィールドに複数の値を追加します。

配列要素の数を制限します。$each 修飾子を使用する必要があります。

配列の要素を順序付けます。$each 修飾子を使用する必要があります。

Specifies the location in the array at which to insert the new elements. Requires the use of the $each modifier. Without the $position modifier, the $push appends the elements to the end of the array.

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:

  1. 配列を更新して、正しい位置に要素を追加します。

  2. 指定されている場合は、ソートします。

  3. 指定されている場合は、配列をスライスします。

  4. 配列を保存します。

このページの例では、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
}

次のドキュメントを 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 }
]
}