|
8 | 8 |
|
9 | 9 | Specify a :operator:`$min` value to specify the *inclusive* lower |
10 | 10 | bound for a specific index in order to constrain the results of |
11 | | - :method:`find() <db.collection.find()>`. Consider the following |
12 | | - operations on a collection named ``collection`` that has an index |
13 | | - ``{ age: 1 }``: |
| 11 | + :method:`find() <db.collection.find()>`. The :program:`mongo` shell |
| 12 | + provides the :method:`cursor.min()` wrapper method: |
14 | 13 |
|
15 | 14 | .. code-block:: javascript |
16 | 15 |
|
17 | | - db.collection.find()._addSpecial("$min" , { age : 20 } ) |
18 | | - db.collection.find( { $query: {}, $min: { age: 20 } } ) |
| 16 | + db.collection.find( { <query> } ).min( { field1: <min value>, ... fieldN: <min valueN>} ) |
19 | 17 |
|
20 | | - These are equivalent to the following :method:`cursor.min()` method that |
21 | | - may be more familiar to you: |
| 18 | + You can also specify the option with either of the two forms: |
| 19 | + |
| 20 | + .. code-block:: javascript |
| 21 | + |
| 22 | + db.collection.find( { <query> } )._addSpecial( "$min", { field1: <min value1>, ... fieldN: <min valueN> } ) |
| 23 | + db.collection.find( { $query: { <query> }, $min: { field1: <min value1>, ... fieldN: <min valueN> } } ) |
| 24 | + |
| 25 | + The :operator:`$min` specifies the lower bound for *all* keys of a |
| 26 | + specific index *in order*. |
| 27 | + |
| 28 | + Consider the following operations on a collection named |
| 29 | + ``collection`` that has an index ``{ age: 1 }``: |
22 | 30 |
|
23 | 31 | .. code-block:: javascript |
24 | 32 |
|
25 | 33 | db.collection.find().min( { age: 20 } ) |
26 | | - |
27 | | - These operations limits the query to those documents where the field |
28 | | - ``age`` is at least ``20`` using the index ``{ age: 1 }``. You can |
29 | | - explicitly specify the particular index with :operator:`$hint`. |
30 | | - Otherwise, MongoDB selects the index using the fields in the |
31 | | - ``indexbounds``; however, if multiple indexes exist on same fields |
32 | | - with different sort orders, the selection of the index may be |
33 | | - ambiguous. |
34 | | - |
35 | | - Use operation alone or in conjunction with :operator:`$max` to limit |
36 | | - results to a specific range for the same index. |
| 34 | + |
| 35 | + These operations limit the query to those documents where the field |
| 36 | + ``age`` is at least ``20`` using the index ``{ age: 1 }``. |
| 37 | + |
| 38 | + You can explicitly specify the corresponding index with |
| 39 | + :method:`cursor.hint()`. Otherwise, MongoDB selects the index using |
| 40 | + the fields in the ``indexbounds``; however, if multiple indexes |
| 41 | + exist on same fields with different sort orders, the selection of |
| 42 | + the index may be ambiguous. |
| 43 | + |
| 44 | + Consider a collection named ``collection`` that has the following |
| 45 | + two indexes: |
| 46 | + |
| 47 | + .. code-block:: javascript |
| 48 | + |
| 49 | + { age: 1, type: -1 } |
| 50 | + { age: 1, type: 1 } |
| 51 | + |
| 52 | + Without explicitly using :method:`cursor.hint()`, it is unclear |
| 53 | + which index the following operation will select: |
| 54 | + |
| 55 | + .. code-block:: javascript |
| 56 | + |
| 57 | + db.collection.find().min( { age: 20, type: 'C' } ) |
| 58 | + |
| 59 | + You can use :operator:`$min` in conjunction with :operator:`$max` to |
| 60 | + limit results to a specific range for the *same* index, as in the |
| 61 | + following example: |
| 62 | + |
| 63 | + .. code-block:: javascript |
| 64 | + |
| 65 | + db.collection.find().min( { age: 20 } ).max( { age: 25 } ) |
37 | 66 |
|
38 | 67 | .. note:: |
39 | 68 |
|
40 | | - In most cases, you should avoid this operator in favor of |
41 | | - :operator:`$gte`. |
| 69 | + Because :method:`cursor.min()` requires an index on a field, and |
| 70 | + forces the query to use this index, you may prefer the |
| 71 | + :operator:`$gte` operator for the query if possible. Consider the |
| 72 | + following example: |
| 73 | + |
| 74 | + .. code-block:: javascript |
| 75 | + |
| 76 | + db.collection.find( { _id: 7 } ).min( { age: 25 } ) |
| 77 | + |
| 78 | + The query will use the index on the ``age`` field, even if the |
| 79 | + index on ``_id`` may be better. |
0 commit comments