Skip to content

Commit f96f89b

Browse files
committed
DOCS-671 min-max edits
1 parent 0e23384 commit f96f89b

File tree

2 files changed

+150
-170
lines changed

2 files changed

+150
-170
lines changed

source/reference/method/cursor.max.txt

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ cursor.max()
99
The :method:`max() <cursor.max()>` method specifies the *exclusive*
1010
upper bound for a specific index in order to constrain the results
1111
of the :method:`find() <db.collection.find()>` method. The
12-
:method:`max() <cursor.max()>` method provides a straight-forward
13-
way to specify an upper bound on multiple-key index.
12+
:method:`max() <cursor.max()>` method provides a way to specify an
13+
upper bound on compound key indexes.
1414

1515
The :method:`max() <cursor.max()>` method takes the following parameter:
1616

@@ -23,116 +23,107 @@ cursor.max()
2323

2424
{ field1: <max value>, field2: <max value2> ... fieldN:<max valueN>}
2525

26-
The fields correspond to *all* the keys of a particular
27-
index *in order*. :program:`mongod` determines the index
28-
using the fields in the ``indexbounds``; however, you can
29-
explicitly specify the particular index with the
30-
:method:`hint() <cursor.hint()>` method.
31-
32-
.. note::
33-
34-
- The :method:`max() <cursor.max()>` method exist primarily to
35-
support the :program:`mongos` (sharding) process.
36-
37-
- Because the :method:`max() <cursor.max()>` method requires a
38-
corresponding index, it may be preferable to use the
39-
:operator:`$lt` operator in the query instead.
40-
41-
- If you use :method:`max() <cursor.max()>` method with the
42-
:method:`min() <cursor.min()>` method to specify a range, the
43-
index bounds specified in :method:`min() <cursor.min()>` method
44-
and the :method:`max() <cursor.max()>` method must both refer
45-
to the keys of the same index.
46-
47-
- The :method:`max() <cursor.max()>` method is a shell wrapper
48-
around the special operator :operator:`$max`.
26+
The fields correspond to *all* the keys of a particular index
27+
*in order*. You can explicitly specify the particular index
28+
with the :method:`hint() <cursor.hint()>` method. Otherwise,
29+
:program:`mongod` selects the index using the fields in the
30+
``indexbounds``; however, if multiple indexes exist on same
31+
fields with different sort orders, the selection of the index
32+
may be ambiguous.
4933

5034
Consider the following example of the :method:`max() <cursor.max()>`
5135
method:
5236

53-
The examples assume a collection ``contacts`` with the following documents:
37+
The examples assume a collection ``products`` with the following documents:
5438

5539
.. code-block:: javascript
56-
57-
{ "_id" : 1, "fname" : "ant", "lname" : "arctica" }
58-
{ "_id" : 2, "fname" : "bob", "lname" : "cat" }
59-
{ "_id" : 3, "fname" : "house", "lname" : "cat" }
60-
{ "_id" : 4, "fname" : "wild", "lname" : "cat" }
61-
{ "_id" : 7, "fname" : "big", "lname" : "dog" }
62-
{ "_id" : 5, "fname" : "ant", "lname" : "eater" }
63-
{ "_id" : 6, "fname" : "ant", "lname" : "elope" }
6440

65-
The collection has the following three indexes:
41+
{ "_id" : 6, "item" : "apple", "type" : "cortland", "price" : 1.29 }
42+
{ "_id" : 2, "item" : "apple", "type" : "fuji", "price" : 1.99 }
43+
{ "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : 1.99 }
44+
{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : 1.29 }
45+
{ "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : 1.29 }
46+
{ "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : 1.29 }
47+
{ "_id" : 7, "item" : "orange", "type" : "cara cara", "price" : 2.99 }
48+
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : 1.39 }
49+
{ "_id" : 9, "item" : "orange", "type" : "satsuma", "price" : 1.99 }
50+
{ "_id" : 8, "item" : "orange", "type" : "valencia", "price" : 0.99 }
51+
52+
The collection has the following four indexes:
6653

6754
.. code-block:: javascript
6855

69-
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.contacts", "name" : "_id_" }
70-
{ "v" : 1, "key" : { "lname" : 1, "fname" : 1 }, "ns" : "test.contacts", "name" : "lname_1_fname_1" }
71-
{ "v" : 1, "key" : { "lname" : 1 }, "ns" : "test.contacts", "name" : "lname_1" }
56+
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.products", "name" :"_id_" },
57+
{ "v" : 1, "key" : { "item" : 1, "type" : 1 }, "ns" :"test.products", "name" : "item_1_type_1" },
58+
{ "v" : 1, "key" : { "item" : 1, "type" : -1 }, "ns" : "test.products", "name" : "item_1_type_-1" }
59+
{ "v" : 1, "key" : { "price" : 1 }, "ns" : "test.products", "name" : "price_1" }
7260

73-
- Specify a lower, inclusive bounds for the index
74-
``lname_1_fname_1`` in the query execution. Using the ordering of
75-
``lname_1_fname_1``, the :method:`max() <cursor.max()>` method
76-
limits the query to the documents that are below the bound of
77-
``lname`` equal to ``dog`` and ``fname`` equal to ``big``:
61+
- Using the ordering of ``item_1_type_1`` index, the :method:`max()
62+
<cursor.max()>` method limits the query to the documents that are
63+
below the bound of ``item`` equal to ``apple`` and ``type`` equal
64+
to ``jonagold``:
7865

7966
.. code-block:: javascript
8067

81-
db.contacts.find().max( { lname: 'dog', fname: 'big' } )
68+
db.products.find().max( { item: 'apple', type: 'jonagold' } ).hint( { item: 1, type: 1 } )
8269

8370
The query returns the following documents:
8471

8572
.. code-block:: javascript
8673

87-
{ "_id" : 1, "fname" : "ant", "lname" : "arctica" }
88-
{ "_id" : 2, "fname" : "bob", "lname" : "cat" }
89-
{ "_id" : 3, "fname" : "house", "lname" : "cat" }
90-
{ "_id" : 4, "fname" : "wild", "lname" : "cat" }
91-
92-
The equivalent query with the :operator:`$lt` operator is:
74+
{ "_id" : 6, "item" : "apple", "type" : "cortland", "price" : 1.29 }
75+
{ "_id" : 2, "item" : "apple", "type" : "fuji", "price" : 1.99 }
76+
{ "_id" : 1, "item" : "apple", "type" : "honey crisp", "price" : 1.99 }
9377

94-
.. code-block:: javascript
78+
If the query did not explicitly specify the index with the
79+
:method:`hint() <cursor.hint()>` method, it is ambiguous as to
80+
whether :program:`mongod` would select the ``{ item: 1, type: 1
81+
}`` index ordering or the ``{ item: 1, type: -1 }`` index ordering.
9582

96-
db.contacts.find( { lname: { $lt: 'dog' } } )
83+
- Using the ordering of the index ``price_1``, the :method:`max()
84+
<cursor.max()>` method limits the query to the documents that are
85+
below the index key bound of ``price`` equal to ``1.99`` and the
86+
:method:`min() <cursor.min()>` method limits the query to the
87+
documents that are at or above the index key bound of ``price``
88+
equal to ``1.39``:
9789

98-
Note that the equivalent query with the :operator:`$lt` operator
99-
is **not**:
100-
10190
.. code-block:: javascript
10291

103-
db.contacts.find( { lname: { $lt: 'dog' } , fname: { $lt: 'big' } } )
92+
db.products.find().min( { price: 1.39 } ).max( { price: 1.99 } ).hint( { price: 1 } )
10493

105-
The query with the :operator:`$lt` operator returns only the
106-
documents with both ``lname`` value less than ``dog`` **and**
107-
``fname`` value less than ``big``:
94+
The query returns the following documents:
10895

10996
.. code-block:: javascript
11097

111-
{ "_id" : 1, "fname" : "ant", "lname" : "arctica" }
98+
{ "_id" : 6, "item" : "apple", "type" : "cortland", "price" : 1.29 }
99+
{ "_id" : 4, "item" : "apple", "type" : "jonathan", "price" : 1.29 }
100+
{ "_id" : 5, "item" : "apple", "type" : "mcintosh", "price" : 1.29 }
101+
{ "_id" : 3, "item" : "apple", "type" : "jonagold", "price" : 1.29 }
102+
{ "_id" : 10, "item" : "orange", "type" : "navel", "price" : 1.39 }
112103

113-
- Specify a range for the index ``lname_1`` in the query execution.
114-
The :method:`min() <cursor.min()>` method limits the query to the
115-
documents that are at or above the index key bound of ``lname``
116-
equal to ``cat`` and the :method:`max() <cursor.max()>` method
117-
limits the query to the documents that are below the index key
118-
bound of ``lname`` equal to ``eater``:
104+
.. note::
119105

120-
.. code-block:: javascript
106+
- Because the :method:`max() <cursor.max()>` method requires a
107+
corresponding index as well as enforces the use of that index,
108+
it may be preferable to use the :operator:`$lt` operator in the
109+
query if possible. Consider the following example:
121110

122-
db.contacts.find().min( { lname: 'cat' } ).max( { lname: 'eater' } )
111+
.. code-block:: javascript
123112

124-
The query returns the following documents:
113+
db.products.find( { _id: 7 } ).max( { price: 1.39 } )
125114

126-
.. code-block:: javascript
115+
The query will use the index on the ``price`` field, even if
116+
the index on ``_id`` may be better.
127117

128-
{ "_id" : 3, "fname" : "house", "lname" : "cat" }
129-
{ "_id" : 2, "fname" : "bob", "lname" : "cat" }
130-
{ "_id" : 4, "fname" : "wild", "lname" : "cat" }
131-
{ "_id" : 7, "fname" : "big", "lname" : "dog" }
118+
- The :method:`max() <cursor.max()>` method exist primarily to
119+
support the :program:`mongos` (sharding) process.
132120

133-
The equivalent query with the :operator:`$gte` and the
134-
:operator:`$lt` operators is:
121+
- If you use :method:`max() <cursor.max()>` method with the
122+
:method:`min() <cursor.min()>` method to specify a range, the
123+
index bounds specified in :method:`min() <cursor.min()>` method
124+
and the :method:`max() <cursor.max()>` method must both refer
125+
to the keys of the same index.
135126

136-
.. code-block:: javascript
127+
- The :method:`max() <cursor.max()>` method is a shell wrapper
128+
around the special operator :operator:`$max`.
137129

138-
db.contacts.find( { lname: { $gte:'cat', $lt: 'eater' } } )

0 commit comments

Comments
 (0)