On this page
$min
在本页面
Definition
$min- $min将字段的值更新为指定的值,如果指定的值小于或小于字段的当前值。 $min运算符可以使用BSON 比较 Sequences比较不同类型的值。
{ $min: { <field1>: <value1>, ... } }
要在嵌入式文档或数组中指定<field>,请使用dot notation。
Behavior
如果该字段不存在,则$min运算符将该字段设置为指定的值。
为了比较不同类型的值(例如数字和空值),$min使用BSON 比较 Sequences。
Examples
使用$ min 比较数字
考虑集合scores中的以下文档:
{ _id: 1, highScore: 800, lowScore: 200 }
该文档的lowScore当前具有值200。以下操作使用$min将200与指定值150比较,并由于150小于200而将lowScore的值更新为150:
db.scores.update( { _id: 1 }, { $min: { lowScore: 150 } } )
scores集合现在包含以下修改后的文档:
{ _id: 1, highScore: 800, lowScore: 150 }
由于字段lowScore的当前值,即150小于250,因此下一个操作无效:
db.scores.update( { _id: 1 }, { $min: { lowScore: 250 } } )
该文档在scores集合中保持不变:
{ _id: 1, highScore: 800, lowScore: 150 }
使用$ min 来比较日期
考虑集合tags中的以下文档:
{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}
以下操作将dateEntered字段的当前值,即ISODate("2013-10-01T05:00:00Z")与指定的日期new Date("2013-09-25")进行比较,以确定是否更新该字段:
db.tags.update(
{ _id: 1 },
{ $min: { dateEntered: new Date("2013-09-25") } }
)
该操作将更新dateEntered字段:
{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-09-25T00:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}