发布于 2015-09-14 15:17:49 | 101 次阅读 | 评论: 0 | 来源: 网络整理

$max

Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find(). The mongo shell provides the cursor.max() wrapper method:

db.collection.find( { <query> } ).max( { field1: <max value>, ... fieldN: <max valueN> } )

You can also specify the option with either of the two forms:

db.collection.find( { <query> } )._addSpecial( "$max", { field1: <max value1>, ... fieldN: <max valueN> } )
db.collection.find( { $query: { <query> }, $max: { field1: <max value1>, ... fieldN: <max valueN> } } )

The $max specifies the upper bound for all keys of a specific index in order.

Consider the following operations on a collection named collection that has an index { age: 1 }:

db.collection.find( { <query> } ).max( { age: 100 } )

This operation limits the query to those documents where the field age is less than 100 using the index { age: 1 }.

You can explicitly specify the corresponding index with cursor.hint(). Otherwise, MongoDB selects the index using the fields in the indexBounds; however, if multiple indexes exist on same fields with different sort orders, the selection of the index may be ambiguous.

Consider a collection named collection that has the following two indexes:

{ age: 1, type: -1 }
{ age: 1, type: 1 }

Without explicitly using cursor.hint(), MongoDB may select either index for the following operation:

db.collection.find().max( { age: 50, type: 'B' } )

Use $max alone or in conjunction with $min to limit results to a specific range for the same index, as in the following example:

db.collection.find().min( { age: 20 } ).max( { age: 25 } )

注解

Because cursor.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:

db.collection.find( { _id: 7 } ).max( { age: 25 } )

The query uses the index on the age field, even if the index on _id may be better.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务