min and max Query Specifiers

The min() and max() functions may be used in conjunction with an index to constrain query matches to those having index keys between the min and max keys specified. The min() and max() functions may be used individually or in conjunction. The index to be used may be specified with a hint() or one may be inferred from pattern of the keys passed to min() and/or max().

db.f.find().min({name:"barry"}}.max({name:"larry"}).hint({name:1});
db.f.find().min({name:"barry"}}.max({name:"larry"});
db.f.find().min({last_name:"smith",first_name:"john"}};

If you're using the standard query syntax, you must distinguish between the $min and $max keys and the query selector itself. See here:

db.f.find({$min: {name:"barry"}, $max: {name:"larry"}, $query:{}});

The min() value is included in the range and the max() value is excluded.

Normally, it is much preferred to use $gte and $lt rather than to use min and max, as min and max require a corresponding index.  Min and max are primarily useful for compound keys: it is difficult to express the last_name/first_name example above without this feature (it can be done using $where).

min and max exist primarily to support the mongos (sharding) process.


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus