Skip to content

Commit a37b542

Browse files
author
Sam Kleinman
committed
merge DOCS-799
2 parents d82210e + 4397a2a commit a37b542

File tree

14 files changed

+261
-104
lines changed

14 files changed

+261
-104
lines changed

source/administration/indexes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ all documents in a collection for documents that match the query.
4444
db.records.find( { user_id: 2 } )
4545

4646
However, the following query, on the ``profile_url`` field is not
47-
supported by this index::
47+
supported by this index:
4848

4949
.. code-block:: javascript
5050

source/core/read-operations.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ In the :program:`mongo` shell, the :method:`find()
3434
<db.collection.find()>` and :method:`findOne()
3535
<db.collection.findOne()>` methods perform read operations. The
3636
:method:`find() <db.collection.find()>` method has the following
37-
syntax:
37+
syntax [#formal-query-structure]_:
3838

3939
.. code-block:: javascript
4040

@@ -69,7 +69,7 @@ syntax:
6969
necessarily consistent unless you specify a sort (:method:`sort()
7070
<cursor.sort()>`).
7171

72-
For example, the following operation on the ``inventory`` collection,
72+
For example, the following operation on the ``inventory`` collection
7373
selects all documents where the ``type`` field equals ``'food'`` and
7474
the ``price`` field has a value less than ``9.95``. The projection
7575
limits the response to the ``item`` and ``qty``, and ``_id`` field:
@@ -93,6 +93,20 @@ For additional documentation and examples of the main MongoDB read
9393
operators, refer to the :doc:`/applications/read` page of the
9494
:doc:`/crud` section.
9595

96+
.. [#formal-query-structure]
97+
:method:`db.collection.find( \<query\>, \<projection\> )
98+
<db.collection.find()>` is a wrapper for the more formal query
99+
structure with the :operator:`$query` operator:
100+
101+
.. code-block:: javascript
102+
103+
db.collection.find( { $query: <query>, ... }, <projection> )
104+
105+
Within the formal structure, in addition to the :operator:`$query`
106+
operator, you can use the :doc:`meta query operators
107+
</reference/meta-query-operators>` to modify the behavior of the
108+
query.
109+
96110
.. _read-operations-query-document:
97111
.. _read-operations-query-argument:
98112

@@ -543,7 +557,7 @@ the query used an index. This query:
543557
- returned 5 documents, as indicated by the :data:`n` field;
544558

545559
- scanned 5 documents from the index, as indicated by the
546-
:data:`nscanned`` field;
560+
:data:`nscanned` field;
547561

548562
- then read 5 full documents from the collection, as indicated by
549563
the :data:`nscannedObjects` field.

source/reference/meta-query-operators.txt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ Introduction
1111

1212
In addition to the :doc:`MongoDB Query Operators
1313
</reference/operators>`, there are a number of "meta" operators that
14-
you may use to modify the output or behavior of a
15-
query. Specify these modifiers to a :method:`db.collection.find()` query, in the
16-
following form (for the :program:`mongo` shell):
14+
you may use to modify the output or behavior of a query. On the server,
15+
MongoDB treats the query and the options as a single object. The
16+
:program:`mongo` shell may provide :ref:`cursor methods
17+
<js-query-cursor-methods>` for these options. When possible, use these
18+
methods; otherwise, you can add these options using either of the
19+
following syntax:
1720

1821
.. code-block:: javascript
1922

20-
db.collection.find( { [QUERY] } )._addSpecial( [MODIFIER] )
21-
22-
Here, the query specified by ``[QUERY]`` runs on the collection
23-
named ``collection`` with the operation specified by the
24-
``[MODIFIER]``. The results are then processed by a modifier expression
25-
selected from the following list. Many of the operators have
26-
corresponding :doc:`methods in the shell </reference/javascript>`. For
27-
the proposes of this reference, this document assumes the above form
28-
where possible.
23+
db.collection.find( { <query> } )._addSpecial( <option> )
24+
db.collection.find( { $query: { <query> }, <option> } )
2925

3026
Modifiers
3127
---------
3228

29+
Many of these operators have corresponding :ref:`methods in the shell
30+
<js-query-cursor-methods>`. These methods provide a straightforward and
31+
user-friendly interface and are the preferred way to add these options.
32+
3333
.. include:: operator/returnKey.txt
3434
:start-after: mongodb
3535

@@ -42,6 +42,9 @@ Modifiers
4242
.. include:: operator/comment.txt
4343
:start-after: mongodb
4444

45+
.. include:: operator/max.txt
46+
:start-after: mongodb
47+
4548
.. include:: operator/min.txt
4649
:start-after: mongodb
4750

source/reference/operator/comment.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ $comment
77
.. operator:: $comment
88

99
The :operator:`$comment` makes it possible to attach a comment to a
10-
query. Because these comments propagate to the
11-
:dbcommand:`profile` log, adding :operator:`$comment` modifiers can
12-
make your profile data much easier to interpret and trace. Consider
13-
the following example:
10+
query. Because these comments propagate to the :dbcommand:`profile`
11+
log, adding :operator:`$comment` modifiers can make your profile
12+
data much easier to interpret and trace. Use one of the following
13+
forms:
1414

1515
.. code-block:: javascript
1616

17-
db.collection.find()._addSpecial( "$comment" , "[COMMENT]" )
18-
19-
Here, ``[COMMENT]`` represents the text of the comment.
17+
db.collection.find( { <query> } )._addSpecial( "$comment", <comment to add> )
18+
db.collection.find( { $query: { <query> }, $comment: <comment to add> } )

source/reference/operator/explain.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ $explain
66

77
.. operator:: $explain
88

9-
Use the :operator:`$explain` operator to return a :term:`document`
10-
that describes the process and indexes used to return the
11-
query. This may provide useful insight when attempting to optimize
12-
a query. Consider the following example:
9+
:operator:`$explain` operator provides information on the query
10+
plan. It returns a :ref:`document <explain-output>` that describes
11+
the process and indexes used to return the query. This may provide
12+
useful insight when attempting to optimize a query.
13+
14+
The :program:`mongo` shell provides the :method:`cursor.explain()`
15+
method:
1316

1417
.. code-block:: javascript
1518

16-
db.collection.find()._addSpecial( "$explain", 1 )
19+
db.collection.find().explain()
1720

18-
The JavaScript function :method:`cursor.explain()` provides equivalent
19-
functionality in the :program:`mongo` shell. See the following
20-
example, which is equivalent to the above:
21+
You can also specify the option in either of the following forms:
2122

2223
.. code-block:: javascript
2324

24-
db.collection.find().explain()
25+
db.collection.find()._addSpecial( "$explain", 1 )
26+
db.collection.find( { $query: {}, $explain: 1 } )

source/reference/operator/hint.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ $hint
66

77
.. operator:: $hint
88

9-
Use the :operator:`$hint` operator to force the query optimizer to
10-
use a specific index to fulfill the query. Use :operator:`$hint`
11-
for testing query performance and indexing strategies. Consider
12-
the following form:
9+
The :operator:`$hint` operator forces the query optimizer to use a
10+
specific index to fulfill the query. You can use :operator:`$hint`
11+
for testing query performance and indexing strategies. The
12+
:program:`mongo` shell provides the :method:`cursor.hint()` method:
1313

1414
.. code-block:: javascript
1515

16-
db.collection.find()._addSpecial( "$hint", { _id : 1 } )
16+
db.collection.find().hint( { age: 1 } )
1717

1818
This operation returns all documents in the collection named
19-
``collection`` using the index on the ``_id`` field. Use this
19+
``collection`` using the index on the ``age`` field. Use this
2020
operator to override MongoDB's default index selection process and
2121
pick indexes manually.
22+
23+
You can also specify the option in either of the following forms:
24+
25+
.. code-block:: javascript
26+
27+
db.collection.find()._addSpecial( "$hint", { age : 1 } )
28+
db.collection.find( { $query: {}, $hint: { age : 1 } } )

source/reference/operator/max.txt

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,74 @@ $max
66

77
.. operator:: $max
88

9-
Specify a :operator:`$max` value to specify an upper boundary for
10-
the value of a field. :program:`mongod` enforces this boundary with
11-
an index of that field.
9+
Specify a :operator:`$max` value to specify the *exclusive* upper
10+
bound for a specific index in order to constrain the results of
11+
:method:`find() <db.collection.find()>`. The :program:`mongo` shell
12+
provides the :method:`cursor.max()` wrapper method:
1213

1314
.. code-block:: javascript
1415

15-
db.collection.find()._addSpecial("$max" , { value : 100 })
16+
db.collection.find( { <query> } ).max( { field1: <max value>, ... fieldN: <max valueN> } )
1617

17-
This operation above limits the documents returned to those that
18-
match the query described by ``[QUERY]`` where the field
19-
``value`` is less than ``20``. :program:`mongod` infers the
20-
index based on on the ``query`` unless specified by the
21-
:method:`cursor.hint()` function.
18+
You can also specify the option with either of the two forms:
2219

23-
Use operation alone or in conjunction with :operator:`$min`
24-
to limit results to a specific range.
20+
.. code-block:: javascript
21+
22+
db.collection.find( { <query> } )._addSpecial( "$max", { field1: <max value1>, ... fieldN: <max valueN> } )
23+
db.collection.find( { $query: { <query> }, $max: { field1: <max value1>, ... fieldN: <max valueN> } } )
24+
25+
The :operator:`$max` specifies the upper 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 }``:
30+
31+
.. code-block:: javascript
32+
33+
db.collection.find( { <query> } ).max( { age: 100 } )
34+
35+
This operation above limits the query to those documents where the
36+
field ``age`` is less than ``100`` 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().max( { age: 50, type: 'B' } )
58+
59+
Use operation alone or in conjunction with :operator:`$min` to limit
60+
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 } )
2566

2667
.. note::
2768

28-
In most cases, you should avoid this operator in favor of
29-
:operator:`$lt`.
69+
Because :method:`cursor.max()` requires an index on a field, and
70+
forces the query to use this index, you may prefer the
71+
:operator:`$lt` operator for the query if possible. Consider the
72+
following example:
73+
74+
.. code-block:: javascript
75+
76+
db.collection.find( { _id: 7 } ).max( { age: 25 } )
77+
78+
The query will use the index on the ``age`` field, even if the
79+
index on ``_id`` may be better.

source/reference/operator/maxScan.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ $maxScan
77
.. operator:: $maxScan
88

99
Constrains the query to only scan the specified number of documents
10-
when fulfilling the query. Use the following form:
10+
when fulfilling the query. Use one of the following forms:
1111

1212
.. code-block:: javascript
1313

14-
db.collection.find()._addSpecial( "$maxScan" , 50 )
14+
db.collection.find( { <query> } )._addSpecial( "$maxScan" , <number> )
15+
db.collection.find( { $query: { <query> }, $maxScan: <number> } )
1516

1617
Use this modifier to prevent potentially long running queries from
1718
disrupting performance by scanning through too much data.

source/reference/operator/min.txt

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,74 @@ $min
66

77
.. operator:: $min
88

9-
Specify a :operator:`$min` value to specify a lower boundary for
10-
the value of a field. :program:`mongod` enforces this boundary with
11-
an index of the field.
9+
Specify a :operator:`$min` value to specify the *inclusive* lower
10+
bound for a specific index in order to constrain the results of
11+
:method:`find() <db.collection.find()>`. The :program:`mongo` shell
12+
provides the :method:`cursor.min()` wrapper method:
1213

1314
.. code-block:: javascript
1415

15-
db.collection.find( { [QUERY] } )._addSpecial("$min" , { value : 20})
16+
db.collection.find( { <query> } ).min( { field1: <min value>, ... fieldN: <min valueN>} )
1617

17-
This operation above limits the documents returned to those that
18-
match the query described by ``[QUERY]`` where the field
19-
``value`` is at least ``20``. :program:`mongod` infers the
20-
index based on the ``query`` unless specified by the
21-
:method:`cursor.hint()` function.
18+
You can also specify the option with either of the two forms:
2219

23-
Use operation alone or in conjunction with :operator:`$max`
24-
to limit results to a specific range.
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 }``:
30+
31+
.. code-block:: javascript
32+
33+
db.collection.find().min( { age: 20 } )
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 } )
2566

2667
.. note::
2768

28-
In most cases, you should avoid this operator in favor of
29-
: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

Comments
 (0)