On this page本页内容
cursor.min()
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Specifies the inclusive lower bound for a specific index in order to constrain the results of 指定特定索引的包含下限,以约束find()
. find()
的结果。min()
provides a way to specify lower bounds on compound key indexes.提供了一种指定复合键索引下限的方法。
The min()
method has the following parameter:min()
方法具有以下参数:
indexBounds | document | The inclusive lower bound for the index keys. |
The indexBounds
parameter has the following prototype form:indexBounds
参数具有以下原型形式:
{ field1: <min value>, field2: <min value2>, fieldN:<min valueN> }
Starting in MongoDB 4.2, you must explicitly specify the particular index with the 从MongoDB 4.2开始,必须使用hint()
method to run min()
with the following exception: you do not need to hint if the find()
query is an equality condition on the _id
field { _id: <value> }
.hint()
方法显式指定特定索引才能运行min()
,但出现以下异常:如果find()
查询是_id
字段{ _id: <value> }
上的相等条件,则无需提示。
In previous versions, you could run 在以前的版本中,可以在有或无显式提示索引的情况下运行min()
with or without explicitly hinting the index. min()
。If run without the hint in 4.0 and earlier, MongoDB selects the index using the fields in the 如果在4.0及更早版本中运行时没有提示,MongoDB将使用indexBounds
; however, if multiple indexes exist on same fields with different sort orders, the selection of the index may be ambiguous.indexBounds
中的字段选择索引;但是,如果同一字段上存在多个索引,且排序顺序不同,则索引的选择可能不明确。
min()
exists primarily to support the 存在主要是为了支持mongos
process.mongos
进程。
Because 因为min()
requires an index on a field, and forces the query to use this index, you may prefer the $gte
operator for the query if possible. min()
需要字段上的索引,并强制查询使用该索引,所以如果可能的话,您可能更喜欢使用$gte
运算符进行查询。Consider the following example:考虑以下示例:
db.products.find( { $in: [ 6, 7 ] } ).min( { price: NumberDecimal("1.39") } ).hint( { price: 1 })
The query will use the index on the 查询将使用price
field, even if the index on _id
may be better.price
字段上的索引,即使_id
上的索引可能更好。
If you use 如果使用min()
with max()
to specify a range:min()
和max()
指定范围:
min()
max()
The min()
and max()
methods indicate that the system should avoid normal query planning. min()
和max()
方法表明系统应该避免正常的查询规划。They construct an index scan where the index bounds are explicitly specified by the values given in 它们构造一个索引扫描,其中索引边界由min()
and max()
.min()
和max()
中给定的值显式指定。
If one of the two boundaries is not specified, the query plan will be an index scan that is unbounded on one side. 如果未指定两个边界中的一个,则查询计划将是一个在一侧无边界的索引扫描。This may degrade performance compared to a query containing neither operator, or one that uses both operators to more tightly constrain the index scan.与既不包含运算符,也不包含运算符以更严格地约束索引扫描的查询相比,这可能会降低性能。
Starting in MongoDB 4.2, you must explicitly specify the particular index with the 从MongoDB 4.2开始,必须使用hint()
method to run min()
with the following exception: you do not need to hint if the find()
query is an equality condition on the _id
field { _id: <value> }
.hint()
方法显式指定特定索引才能运行min()
,但出现以下异常:如果find()
查询是_id
字段{ _id: <value> }
上的相等条件,则无需提示。
For the examples below, create a sample collection named 对于以下示例,创建一个名为products
that holds the following documents:products
的示例集合,其中包含以下文档:
db.products.insertMany([ { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : NumberDecimal("1.99") }, { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : NumberDecimal("1.99") }, { "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : NumberDecimal("1.29") }, { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : NumberDecimal("1.29") }, { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : NumberDecimal("1.29") }, { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : NumberDecimal("1.29") }, { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : NumberDecimal("2.99") }, { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : NumberDecimal("1.99") }, { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : NumberDecimal("0.99") }, { "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") } ])
Create the following indexes for the collection:为集合创建以下索引:
db.products.createIndexes( [ { "item" : 1, "type" : 1 }, { "item" : 1, "type" : -1 }, { "price" : 1 } ] )
Using the ordering of the 通过使用{ item: 1, type: 1 }
index, min()
limits the query to the documents that are at or above the index key bound of item
equal to apple
and type
equal to jonagold
, as in the following:{item:1, type:1}
索引的顺序,min()
将查询限制在item
等于apple
,type
等于jonagold
的索引键界限或之上的文档,如下所示:
db.products.find().min( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )
The query returns the following documents:查询返回以下文档:
{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : NumberDecimal("1.29") } { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : NumberDecimal("1.29") } { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : NumberDecimal("1.29") } { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : NumberDecimal("2.99") } { "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") } { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : NumberDecimal("1.99") } { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : NumberDecimal("0.99") }
Using the ordering of the index 使用索引{ price: 1 }
, min()
limits the query to the documents that are at or above the index key bound of price
equal to 1.39
and max()
limits the query to the documents that are below the index key bound of price
equal to 1.99
:{price:1}
的顺序,min()
将查询限制为price
等于1.39
的索引键界限或以上的文档,max()
将搜索限制为price
小于1.99
的索引键界限的文档:
db.products.find().min( { price: NumberDecimal("1.39") } ).max( { price: NumberDecimal("1.99") } ).hint( { price: 1 } )
The query returns the following documents:查询返回以下文档:
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }