ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

$max

項目一覧

$max

The $max operator updates the value of the field to a specified value if the specified value is greater than the current value of the field. The $max operator can compare values of different types, using the BSON comparison order.

The $max operator expression has the form:

{ $max: { <field1>: <value1>, ... } }

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

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

If the field does not exists, the $max operator sets the field to the specified value.

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

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

db.scores.insertOne( { _id: 1, highScore: 800, lowScore: 200 } )

The highScore for the document currently has the value 800. The following operation:

  • Compares the highscore, 800, to the specified value, 950

  • Updates highScore to 950 since 950 is greater than 800

db.scores.updateOne( { _id: 1 }, { $max: { highScore: 950 } } )

scoresコレクションには、次の変更されたドキュメントが含まれています。

{ _id: 1, highScore: 950, lowScore: 200 }

The next operation has no effect since the value of highScore, 950, is greater than 870:

db.scores.updateOne( { _id: 1 }, { $max: { highScore: 870 } } )

scoresコレクションではドキュメントは変更されていません。

{ _id: 1, highScore: 950, lowScore: 200 }

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

db.tags.insertOne(
{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")
}
)

The following operation compares the current value of the dateExpired field, ISODate("2013-10-01T16:38:16.163Z"), with the specified date new Date("2013-09-30") to determine whether to update the field:

db.tags.updateOne(
{ _id: 1 },
{ $max: { dateExpired: new Date("2013-09-30") } }
)

new Date("2013-09-30") is not the newest date, so the operation does ではない update the dateExpired field:

{
_id: 1,
desc: "decorative arts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")
}

以下も参照してください。

項目一覧