$max
On this page本页内容
Definition定义
$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.$max
运算符会将字段的值更新为指定值。The$max
operator can compare values of different types, using the BSON comparison order.$max
运算符可以使用BSON比较顺序比较不同类型的值。The$max
operator expression has the form:$max
运算符表达式的形式为:{ $max: { <field1>: <value1>, ... } }
To specify a要在嵌入文档或数组中指定<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
Behavior行为
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,update运算符按照字典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. See Update Operators Behavior for details.具有数字名称的字段按数字顺序处理。有关详细信息,请参阅更新运算符行为。
If the field does not exists, the 如果该字段不存在,$max
operator sets the field to the specified value.$max
运算符会将该字段设置为指定值。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当您将mongod
no longer raises an error when you use an update operator like $max
with an empty operand expression ( { }
). $max
之类的更新运算符与空操作数表达式({ }
)一起使用时,mongod
不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
Examples实例
Use $max
to Compare Numbers使用$max
比较数字
$max
to Compare NumbersCreate the 创建scores
collection:scores
集合:
db.scores.insertOne( { _id: 1, highScore: 800, lowScore: 200 } )
The 文档的highScore
for the document currently has the value 800. highScore
当前的值为800。The following operation:以下操作:
Compares the将highscore
, 800, to the specified value, 950highscore
800与指定值950进行比较Updates将highScore
to 950 since 950 is greater than 800highScore
更新为950,因为950大于800
db.scores.updateOne( { _id: 1 }, { $max: { highScore: 950 } } )
The scores
collection now contains the following modified document:scores
集合现在包含以下修改后的文档:
{ _id: 1, highScore: 950, lowScore: 200 }
The next operation has no effect since the value of 下一个操作没有效果,因为highScore
, 950, is greater than 870:highScore
的值950
大于870
:
db.scores.updateOne( { _id: 1 }, { $max: { highScore: 870 } } )
The document remains unchanged in the 该文档在scores
collection:scores
集合中保持不变:
{ _id: 1, highScore: 950, lowScore: 200 }
Use $max
to Compare Dates使用$max
比较日期
$max
to Compare DatesCreate the 创建tags
collection: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:dateExpired
字段ISODate("2013-10-01T16:38:16.163Z")
的当前值与指定的new Date("2013-09-30")
进行比较,以确定是否更新该字段:
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 not update the 不是最新日期,因此操作不会更新dateExpired
field:dateExpired
字段:
{
_id: 1,
desc: "decorative arts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16.163Z")
}