文档菜单
文档首页
/
MongoDB 手册
/ / /

cursor.max()

本页内容

  • 定义
  • 兼容性
  • 行为
  • 示例
cursor.max()

重要

mongosh方法

本页文档说明了一个mongosh方法。这不是特定语言驱动的文档,例如Node.js。

有关MongoDB API驱动程序的文档,请参阅特定语言的MongoDB驱动程序文档。

指定特定索引的独占上限,以约束find() 的结果。max() 提供了一种指定复合键索引上限的方法。

max() 方法有以下参数

参数
类型
描述
indexBounds
文档
索引键的独占上限。

indexBounds 参数具有以下原型形式

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

字段对应于特定索引的所有键的顺序。

注意

索引使用

要使用索引与 max() 方法,您必须使用 hint() 方法指定要使用的索引,除非 find() 查询是 _id 字段的等价条件。

提示

另请参阅

max() 主要用于支持 mongos(分片)进程。

此方法在以下环境中提供

  • MongoDB Atlas:云中MongoDB部署的完全托管服务

注意

此命令在所有MongoDB Atlas集群中均受支持。有关所有命令的Atlas支持信息,请参阅不受支持的命令。

因为 max() 需要在字段上有一个索引,并强制查询使用此索引,如果可能的话,您可能更喜欢使用 $lt 操作符进行查询。考虑以下示例

db.products.find( { _id: { $in: [ 6, 7 ] } } ).max( { price: NumberDecimal("1.39") } ).hint( { price: 1 } )

查询将使用 price 字段的索引,即使 _id 字段的索引可能更好。

如果您使用 max()min() 来指定范围

  • min()max() 中指定的索引边界必须都引用同一索引的键。

  • max() 指定的边界必须大于 min() 指定的边界。

min()max() 方法表示系统应避免正常查询计划。它们构建一个索引扫描,其中索引边界由 min()max() 给出的值显式指定。

警告

如果两个边界中没有指定一个,查询计划将是一个在一侧无界的索引扫描。这可能与不包含这两个操作符的查询相比,或者与使用这两个操作符更紧密约束索引扫描的查询相比,性能降低。

注意

您必须显式指定特定的索引,使用 hint() 方法来运行 max(),但有以下例外:如果 find() 查询是对 _id 字段的等值条件 { _id: <value> },则不需要提示。

以下示例中,创建一个名为 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") }
])

为该集合创建以下索引

db.products.createIndexes( [
{ "item" : 1, "type" : 1 },
{ "item" : 1, "type" : -1 },
{ "price" : 1 }
] )
  • 使用索引顺序 { item: 1, type: 1 }max() 将查询限制在以下文档中:文档的 item 等于 appletype 等于 jonagold 的边界以下。

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

    查询返回以下文档

    { "_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") }
  • 使用索引顺序 { price: 1 }max() 将查询限制在索引键边界 price 等于 NumberDecimal("1.99") 以下的文档,而 min() 将查询限制在索引键边界 price 等于 NumberDecimal("1.39") 或以上的文档。

    注意

    max() 指定的边界必须大于由 min() 指定的边界。

    db.products.find().min( { price: NumberDecimal("1.39") } ).max( { price: NumberDecimal("1.99") } ).hint( { price: 1 } )

    查询返回以下文档

    { "_id" : 10, "item" : "orange", "type" : "navel", "price" : NumberDecimal("1.39") }

返回

cursor.map

© . This site is unofficial and not affiliated with MongoDB, Inc.