Docs HomeMongoDB Manual

cursor.max()

On this page本页内容

Definition定义

cursor.max()
Important

mongosh Method

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

Specifies the exclusive upper bound for a specific index in order to constrain the results of find(). 指定特定索引的独占上限,以便约束find()的结果。max() provides a way to specify an upper bound on compound key indexes.提供了一种指定复合键索引上限的方法。

Parameters参数

The max() method has the following parameter:max()方法具有以下参数:

Parameter参数Type类型Description描述
indexBoundsdocumentThe exclusive upper bound for the index keys.索引键的独占上限。

The indexBounds parameter has the following prototype form:indexBounds参数具有以下原型形式:

{ field1: <max value>, field2: <max value2> ... fieldN:<max valueN> }

The fields correspond to all the keys of a particular index in order.字段按顺序对应于特定索引的所有键。

Note

Index Use索引使用

To use an index with the max() method, you must use the hint() method to specify the index you want to use, except when the find() query is an equality condition on the _id field.要将索引与max()方法一起使用,必须使用hint()方法指定要使用的索引,除非find()查询是_id字段上的相等条件。

Tip

See also: 另请参阅:

min()

max() exists primarily to support the mongos (sharding) process.存在主要是为了支持mongos(分片)过程。

Behavior行为

Interaction with Index Selection与索引选择的交互

Because max() requires an index on a field, and forces the query to use this index, you may prefer the $lt operator for the query if possible. Consider the following example:由于max()需要字段上的索引,并强制查询使用此索引,因此如果可能的话,您可能更喜欢$lt运算符作为查询。考虑以下示例:

db.products.find( { _id: { $in: [ 6, 7 ] } } ).max( { 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上的索引可能更好。

Index Bounds索引边界

If you use max() with min() to specify a range:如果使用max()min()来指定范围:

  • the index bounds specified in min() and max() must both refer to the keys of the same index.min()max()中指定的索引边界必须都引用同一索引的键。
  • the bound specified by max() must be greater than the bound specified by min().max()指定的绑定必须大于由min()规定的绑定。

max() without 而不用min()

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()中给定的值显式指定。

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.与既不包含运算符也不使用两个运算符来更严格地约束索引扫描的查询相比,这可能会降低性能。

Example实例

Note

Starting in MongoDB 4.2, you must explicitly specify the particular index with the hint() method to run max() with the following exception: you do not need to hint if the find() query is an equality condition on the _id field { _id: <value> }.从MongoDB 4.2开始,您必须使用hint()方法显式指定特定索引才能运行max(),但有以下例外:如果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 { item: 1, type: 1 } index, max() limits the query to the documents that are below the bound of item equal to apple and type equal to jonagold:使用{ item: 1, type: 1 }索引的排序,max()将查询限制在item等于appletype等于jonagold的边界以下的文档:

    db.products.find().max( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )

    The query returns the following documents:查询返回以下文档:

    { "_id" : 6, "item" : "apple", "type" : "cortland", "price" : NumberDecimal("1.29") }
    { "_id" : 2, "item" : "apple", "type" : "fuji", "price" : NumberDecimal("1.99") }
    { "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : NumberDecimal("1.99") }
  • Using the ordering of the index { price: 1 }, max() limits the query to the documents that are below the index key bound of price equal to NumberDecimal("1.99") and min() limits the query to the documents that are at or above the index key bound of price equal to NumberDecimal("1.39"):使用索引{ price: 1 }的排序,max()将查询限制为低于等于NumberDecimal("1.99")price的索引键界限的文档,min()将搜索限制为处于或高于等于NumberDecimal("1.39")price的索引键边界的文档:

    Note

    The bound specified by max() must be greater than the bound specified by min().max()指定的绑定必须大于由min()所指定的绑定。

    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") }