On this page本页内容
$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
运算符表达式的形式如下:
To specify a 若要在嵌入式文档或数组中指定一个<field>
in an embedded document or in an array, use dot notation.<field>
,请使用点表示法。
If the field does not exists, the 如果该字段不存在,$max
operator sets the field to the specified value.$max
运算符将该字段设置为指定值。
$max
to Compare Numbers$max
来比较数字¶Consider the following document in the collection 在集合scores
:scores
中考虑以下文档:
The 文档的highScore
for the document currently has the value 800
. highScore
当前为800
。The following operation uses 以下操作使用$max
to compare the 800
and the specified value 950
and updates the value of highScore
to 950
since 950
is greater than 800
:$max
比较800
和指定值950
,并将highScore
的值更新为950
,因为950
大于800
:
The scores
collection now contains the following modified document:scores
集合现在包含以下修改过的文档:
The next operation has no effect since the current value of the field 由于字段highScore
, i.e. 950
, is greater than 870
:highScore
(即950
)的当前值大于870
,因此下一个操作无效:
The document remains unchanged in the 该文档在scores
collection:scores
集合中保持不变:
$max
to Compare Dates$max
来比较日期¶Consider the following document in the collection 在集合tags
:tags
中考虑以下文档:
The following operation compares the current value of the 以下操作将dateExpired
field, i.e. 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")
进行比较,以确定是否更新该字段:
The operation does not update the 该操作不会更新dateExpired
field:dateExpired
字段:
See also参阅