Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

cursor.min()

在此页面上

  • 定义
  • 行为
  • 例子
cursor.min()

重要

mongosh 方法

本页面提供 mongosh 方法的相关信息。这不是特定于语言的驱动程序(例如 Node.js)的文档。

如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。

指定特定索引的包含下限,以约束 find()的结果。 min()提供了一种指定复合键索引下限的方法。

min() 方法具有以下参数:

Parameter
类型
说明
indexBounds
文档
索引键的包含下限。

indexBounds参数具有以下原型形式:

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

注意

索引使用

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

提示

另请参阅:

min() 的存在主要是为了支持 mongos 进程。

由于 min() 要求在字段上使用索引,并强制查询使用此索引,因此如果可能,您可能更愿意使用 $gte 操作符进行查询。请考虑以下示例:

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

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

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

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

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

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

警告

如果未指定两个边界之一,则查询计划将是一侧无界的索引扫描。相较于不包含任何操作符的查询,或使用两个操作符更严格地约束索引扫描的查询,这可能会降低性能。

除非 find() 查询是 _id 字段 { _id: <value> } 上的相等条件,否则必须使用 hint() 方法明确指定索引才能运行 min()

对于以下示例,创建一个名为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 } 索引的排序,min() 将查询限制在 item 等于 appletype 等于jonagold 的索引键值范围之内或之上的文档,如下所示:

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

    该查询返回以下文档:

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

    注意

    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.maxTimeMS

在此页面上