On this page本页内容
cursor.min()¶mongo Shell Method
This page documents the 本页记录了mongo shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
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 indexBounds parameter has the following prototype form:indexBounds参数具有以下原型形式:
Index Use
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(),但有以下例外:不需要提示min()查询是否是_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中的字段选择索引;但是,如果同一字段上存在多个排序顺序不同的索引,则索引的选择可能不明确。
See also参阅
min() exists primarily to support the mongos process, and is a shell wrapper around the query modifier $min.min()的存在主要是为了支持mongos进程,是围绕查询修饰符$min的shell包装。
Deprecated since v3.2从v3开始就不推荐了。2.
Starting in v3.2, the 从v3.2开始,$min operator is deprecated in the mongo shell. $min运算符在mongo shell中不受欢迎。In the 在mongo shell, use cursor.min() instead.mongo shell中,请改用cursor.min()。
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:考虑下面的例子:
The query will use the index on the 查询将使用price field, even if the index on _id may be better.price字段上的索引,即使_id上的索引可能更好。
min()max()¶The min and max operators indicate that the system should avoid normal query planning. min和max运算符表示系统应避免正常的查询计划。Instead they construct an index scan where the index bounds are explicitly specified by the values given in 相反,它们构造一个索引扫描,其中索引边界由min and max.min和max中给出的值显式指定。
Warning警告
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的样本集合,其中包含以下文档:
Create the following indexes for the collection:为集合创建以下索引:
{ 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的索引键范围或以上的文档上,如下所示:
The query returns the following documents:查询将返回以下文档:
{ 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的文档上:
Note
The query returns the following documents:查询将返回以下文档: