对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

游标()(mongosh方法)

cursor.min()

重要

mongosh 方法

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

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

Specifies the inclusive lower bound for a specific index in order to constrain the results of find(). min() provides a way to specify lower bounds on compound key indexes.

The min() method has the following parameter:

Parameter
类型
说明

indexBounds

文档

索引键的包含下限。

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

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

注意

索引使用

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

提示

min() exists primarily to support the mongos process.

此方法可用于以下环境中托管的部署:

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

注意

所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

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. Consider the following example:

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

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

If you use min() with max() to specify a range:

  • the index bounds specified in min() and max() must both refer to the keys of the same index.

  • the bound specified by max() must be greater than the bound specified by min().

The min() and max() methods indicate that the system should avoid normal query planning. They construct an index scan where the index bounds are explicitly specified by the values given in min() and max().

警告

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

Unless the find() query is an equality condition on the _id field { _id: <value> }, you must explicitly specify the index with the hint() method to run min().

对于以下示例,创建一个名为products的示例集合,其中包含以下文档:

db.products.insertMany([
{ "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : Decimal128("1.99") },
{ "_id" : 2, "item" : "apple", "type" : "fuji", "price" : Decimal128("1.99") },
{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : Decimal128("1.29") },
{ "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : Decimal128("1.29") },
{ "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : Decimal128("1.29") },
{ "_id" : 6, "item" : "apple", "type" : "cortland", "price" : Decimal128("1.29") },
{ "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : Decimal128("2.99") },
{ "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : Decimal128("1.99") },
{ "_id" : 8, "item" : "orange", "type" : "valencia", "price" : Decimal128("0.99") },
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }
])

为数据集创建以下索引:

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:

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

    该查询返回以下文档:

    { "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : Decimal128("1.29") }
    { "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : Decimal128("1.29") }
    { "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : Decimal128("1.29") }
    { "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : Decimal128("2.99") }
    { "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }
    { "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : Decimal128("1.99") }
    { "_id" : 8, "item" : "orange", "type" : "valencia", "price" : Decimal128("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:

    注意

    The bound specified by max() must be greater than the bound specified by min().

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

    该查询返回以下文档:

    { "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }
给本页内容打分