On this page本页内容
$min
The 如果指定值小于字段的当前值,$min updates the value of the field to a specified value if the specified value is less than the current value of the field. $min将字段的值更新为指定值。The $min operator can compare values of different types, using the BSON comparison order.$min运算符可以使用BSON比较顺序比较不同类型的值。
{ $min: { <field1>: <value1>, ... } }
To specify a 要在嵌入文档或数组中指定<field> in an embedded document or in an array, use dot notation.<field>,请使用点表示法。
Starting in MongoDB 5.0, update operators process document fields with string-based names in lexicographic order. 从MongoDB 5.0开始,更新运算符以词典顺序处理具有基于字符串的名称的文档字段。Fields with numeric names are processed in numeric order. 具有数字名称的字段按数字顺序处理。See Update Operators Behavior for details.有关详细信息,请参阅更新运算符行为。
If the field does not exist, the 如果字段不存在,$min operator sets the field to the specified value.$min运算符将字段设置为指定值。
For comparisons between values of different types, such as a number and a null, 对于不同类型的值(如数字和空值)之间的比较,$min uses the BSON comparison order.$min使用BSON比较顺序。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,当使用带有空操作数表达式(mongod no longer raises an error when you use an update operator like $min with an empty operand expression ( { } ). { })的更新运算符(如$min)时,mongod不再引发错误。An empty update results in no changes and no oplog entry is created (meaning that the operation is a no-op).空更新不会导致任何更改,也不会创建oplog条目(这意味着该操作是无操作)。
$min to Compare Numbers$min比较数字Create the 创建scores collection:scores集合:
db.scores.insertOne( { _id: 1, highScore: 800, lowScore: 200 } )
The 文档的lowScore for the document currently has the value 200. lowScore当前具有值200。The following operation uses 以下操作使用$min to compare 200 to the specified value 150 and updates the value of lowScore to 150 since 150 is less than 200:$min将200与指定值150进行比较,并将lowScore的值更新为150,因为150小于200:
db.scores.updateOne( { _id: 1 }, { $min: { lowScore: 150 } } )
The scores collection now contains the following modified document:scores集合现在包含以下修改的文档:
{ _id: 1, highScore: 800, lowScore: 150 }
The next operation has no effect since the current value of the field 下一个操作没有效果,因为字段lowScore, i.e 150, is less than 250:lowScore的当前值,即150,小于250:
db.scores.updateOne( { _id: 1 }, { $min: { lowScore: 250 } } )
The document remains unchanged in the 文档在scores collection:scores集合中保持不变:
{ _id: 1, highScore: 800, lowScore: 150 }
$min to Compare Dates$min比较日期Create the 创建tags collection:tags集合:
db.tags.insertOne(
{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-10-01T05:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}
)
The following operation compares the current value of the 以下操作将dateEntered field, i.e. ISODate("2013-10-01T05:00:00Z"), with the specified date new Date("2013-09-25") to determine whether to update the field:dateEntered字段的当前值(即ISODate("2013-10-01T05:00:00Z")与指定日期new Date("2013-09-25")进行比较,以确定是否更新该字段:
db.tags.updateOne(
{ _id: 1 },
{ $min: { dateEntered: new Date("2013-09-25") } }
)
The operation updates the 该操作将更新dateEntered field:dateEntered字段:
{
_id: 1,
desc: "crafts",
dateEntered: ISODate("2013-09-25T00:00:00Z"),
dateExpired: ISODate("2013-10-01T16:38:16Z")
}