Definition定义
cursor.min()-
Important
mongosh
Method方法This page documents a本页记录了一种mongoshmethod. This is not the documentation for a language-specific driver, such as Node.js.mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关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.提供了一种指定复合键索引下限的方法。Themin()method has the following parameter:min()方法有以下参数:Parameter参数Type类型Description描述indexBoundsdocument文档The inclusive lower bound for the index keys.索引键的包含下限。TheindexBoundsparameter has the following prototype form:indexBounds参数具有以下原型形式:{ field1: <min value>, field2: <min value2>, fieldN:<min valueN> }
Note
Index Use索引使用
Tip
min() exists primarily to support the mongos process.min()的存在主要是为了支持mongos进程。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Note
This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令。
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Behaviors行为
Interaction with Index Selection与指数选择的互动
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:min()需要字段上的索引,并强制查询使用此索引,如果可能的话,您可能更喜欢查询的$gte运算符。考虑以下示例:
db.products.find( { _id: { $in: [ 6, 7 ] } } ).min( { price: Decimal128("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 如果你使用min() with max() to specify a range:min()和max()来指定一个范围:
min() without 而不用max()
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示例
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().find()查询是_id字段{_id:<value>}的相等条件,否则必须使用hint()方法显式指定索引以运行min()。
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" : 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") }
])
Create the following indexes for the collection:为集合创建以下索引:
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 ofitemequal toappleandtypeequal tojonagold, as in the following:{ item: 1, type: 1 }索引的顺序,min()将查询限制在等于apple的item和等于jonagold的type的索引键绑定处或之上的文档,如下所示:db.products.find().min( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )The query returns the following documents:查询返回以下文档:{ "_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 ofpriceequal to1.39andmax()limits the query to the documents that are below the index key bound ofpriceequal to1.99:{ price: 1 }的顺序,min()将查询限制在等于或高于price等于1.39的索引键界限的文档,max()将搜索限制在低于价格等于1.99的索引键值界限的文档:Note
db.products.find().min( { price: Decimal128("1.39") } ).max( { price: Decimal128("1.99") } ).hint( { price: 1 } )The query returns the following documents:查询返回以下文档:{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : Decimal128("1.39") }