Skip to content

Commit c5bc889

Browse files
committed
DOCS-799 Port mongo query language wiki
1 parent 7bbbd96 commit c5bc889

File tree

14 files changed

+147
-80
lines changed

14 files changed

+147
-80
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

@@ -411,7 +425,7 @@ the query used an index. This query:
411425
- returned 5 documents, as indicated by the :data:`n` field;
412426

413427
- scanned 5 documents from the index, as indicated by the
414-
:data:`nscanned`` field;
428+
:data:`nscanned` field;
415429

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

source/reference/meta-query-operators.txt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,26 @@ In addition to the :doc:`MongoDB Query Operators
1313
</reference/operators>`, there are a number of "meta" operators that
1414
you may use to modify the output or behavior of a
1515
query. Specify these modifiers to a :method:`db.collection.find()` query, in the
16-
following form (for the :program:`mongo` shell):
16+
following forms (for the :program:`mongo` shell):
1717

1818
.. code-block:: javascript
1919

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.
20+
db.collection.find( { <QUERY> } )._addSpecial( <MODIFIER> )
21+
db.collection.find( { $query: { <QUERY> }, <MODIFIER> } )
22+
23+
Here, the query specified by ``<QUERY>`` runs on the collection named
24+
``collection`` with the operation specified by the ``<MODIFIER>``. The
25+
results are then processed by a modifier expression selected from the
26+
following list. MongoDB treats the query and the
27+
modifiers as a single object, with the :operator:`$query` and the meta
28+
operators included in a single document.
2929

3030
Modifiers
3131
---------
3232

33+
Many of the operators have corresponding :doc:`methods
34+
in the shell </reference/javascript>`.
35+
3336
.. include:: operator/returnKey.txt
3437
:start-after: mongodb
3538

@@ -42,6 +45,9 @@ Modifiers
4245
.. include:: operator/comment.txt
4346
:start-after: mongodb
4447

48+
.. include:: operator/max.txt
49+
:start-after: mongodb
50+
4551
.. include:: operator/min.txt
4652
:start-after: mongodb
4753

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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ $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+
Retrieve the query plan by adding the :operator:`$explain` operator
15+
to a :method:`find() <db.collection.find()>` query, as in the
16+
following examples:
1317

1418
.. code-block:: javascript
1519

1620
db.collection.find()._addSpecial( "$explain", 1 )
21+
db.collection.find( { $query: {}, $explain: 1 } )
1722

1823
The JavaScript function :method:`cursor.explain()` provides equivalent
1924
functionality in the :program:`mongo` shell. See the following

source/reference/operator/hint.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ $hint
99
Use the :operator:`$hint` operator to force the query optimizer to
1010
use a specific index to fulfill the query. Use :operator:`$hint`
1111
for testing query performance and indexing strategies. Consider
12-
the following form:
12+
the following operations:
1313

1414
.. code-block:: javascript
1515

1616
db.collection.find()._addSpecial( "$hint", { _id : 1 } )
17+
db.collection.find( { $query: {}, $hint: { _id : 1 } } )
1718

18-
This operation returns all documents in the collection named
19+
These are equivalent to the following :method:`cursor.hint()` method that
20+
may be more familiar to you:
21+
22+
.. code-block:: javascript
23+
24+
db.collection.find().hint( { _id: 1 } )
25+
26+
These operations return all documents in the collection named
1927
``collection`` using the index on the ``_id`` field. Use this
2028
operator to override MongoDB's default index selection process and
2129
pick indexes manually.

source/reference/operator/max.txt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,34 @@ $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()>`. Consider the following
12+
operations on a collection named ``collection`` that has an index
13+
``{ age: 1 }``:
1214

1315
.. code-block:: javascript
1416

15-
db.collection.find()._addSpecial("$max" , { value : 100 })
17+
db.collection.find()._addSpecial( "$max", { age : 100 } )
18+
db.collection.find( { $query: {}, $max: { age: 100 } } )
1619

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.
20+
These are equivalent to the following :method:`cursor.max()` method that
21+
may be more familiar to you:
22+
23+
.. code-block:: javascript
24+
25+
db.collection.find().max( { age: 100 } )
26+
27+
This operation above limits the query to those documents where the
28+
field ``age`` is less than ``100`` using the index ``{ age: 1 }``.
29+
You can explicitly specify the particular index with the
30+
:operator:`$hint`. Otherwise, MongoDB selects the index using the
31+
fields in the ``indexbounds``; however, if multiple indexes exist on
32+
same fields with different sort orders, the selection of the index
33+
may be ambiguous.
2234

2335
Use operation alone or in conjunction with :operator:`$min`
24-
to limit results to a specific range.
36+
to limit results to a specific range for the same index.
2537

2638
.. note::
2739

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: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,34 @@ $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()>`. Consider the following
12+
operations on a collection named ``collection`` that has an index
13+
``{ age: 1 }``:
1214

1315
.. code-block:: javascript
1416

15-
db.collection.find( { [QUERY] } )._addSpecial("$min" , { value : 20})
17+
db.collection.find()._addSpecial("$min" , { age : 20 } )
18+
db.collection.find( { $query: {}, $min: { age: 20 } } )
1619

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.
20+
These are equivalent to the following :method:`cursor.min()` method that
21+
may be more familiar to you:
2222

23-
Use operation alone or in conjunction with :operator:`$max`
24-
to limit results to a specific range.
23+
.. code-block:: javascript
24+
25+
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.
2537

2638
.. note::
2739

source/reference/operator/orderby.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@ $orderby
77
.. operator:: $orderby
88

99
The :operator:`$orderby` operator sorts the results of a query in
10-
ascending or descending order. Consider the following syntax:
10+
ascending or descending order. Consider the following operations:
1111

1212
.. code-block:: javascript
1313

14-
db.collection.find()._addSpecial( "$orderby", { age : -1} )
14+
db.collection.find()._addSpecial( "$orderby", { age : -1 } )
15+
db.collection.find( { $query: {}, $orderby: { age : -1 } } )
1516

16-
This is equivalent to the following :method:`cursor.sort()` method that
17+
These are equivalent to the following :method:`cursor.sort()` method that
1718
may be more familiar to you:
1819

1920
.. code-block:: javascript
2021

2122
db.collection.find().sort( { age: -1 } )
2223

23-
Both of these examples return all documents in the collection named
24-
``collection`` sorted for in descending order from greatest to
25-
smallest. Specify a value to :operator:`$orderby` of negative one
26-
(e.g. ``-1``, as above) to sort in descending order or a positive
27-
value (e.g. ``1``) to sort in ascending order.
24+
These examples return all documents in the collection named
25+
``collection`` sorted by the ``age`` field in descending order.
26+
Specify a value to :operator:`$orderby` of negative one (e.g.
27+
``-1``, as above) to sort in descending order or a positive value
28+
(e.g. ``1``) to sort in ascending order.
2829

2930
Unless you have a index for the specified key pattern, use
3031
:operator:`$orderby` in conjunction with :operator:`$maxScan` and/or
3132
:method:`cursor.limit()` to avoid requiring MongoDB to perform a large
3233
in-memory sort. :method:`cursor.limit()` increases the speed and reduce
3334
the amount of memory required to return this query by way of an
3435
optimized algorithm.
36+
37+
.. seealso:: :operator:`$query` operator

source/reference/operator/query.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ $query
77
.. operator:: $query
88

99
The :operator:`$query` operator provides an interface to describe
10-
queries. Consider the following operation.
10+
queries. Consider the following operation:
1111

1212
.. code-block:: javascript
1313

14-
db.collection.find()._addSpecial( "$query" : { value : 100 } )
14+
db.collection.find( { $query: { age : 25 } } )
1515

1616
This is equivalent to the following :method:`db.collection.find()` method that
1717
may be more familiar to you:
1818

1919
.. code-block:: javascript
2020

21-
db.collection.find( { value : 100 } )
21+
db.collection.find( { age : 25 } )
22+
23+
These operations return only those documents in the collection named
24+
``collection`` where the ``age`` field equals ``25``.

source/reference/operator/returnKey.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ $returnKey
66

77
.. operator:: $returnKey
88

9-
Only return the index key (i.e. :term:`_id`) or keys for the
10-
results of the query. Use the following form:
9+
Only return the index key or keys for the results of the query. If
10+
:operator:`$returnKey` is set to ``true`` and the query does not use
11+
an index to perform the read operation, the returned documents will
12+
not contain any fields. Use one of the following forms:
1113

1214
.. code-block:: javascript
1315

14-
db.collection.find()._addSpecial("$returnKey" , true )
16+
db.collection.find( { <QUERY> } )._addSpecial( "$returnKey", true )
17+
db.collection.find( { $query: { <QUERY> }, $returnKey: true } )

source/reference/operator/showDiskLoc.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ $showDiskLoc
66

77
.. operator:: $showDiskLoc
88

9-
Use the following modifier to display the disk location:
9+
Use the following modifier to display the disk location information
10+
as an additional field ``$diskLoc`` in the returned documents. Use
11+
one of the following forms:
1012

1113
.. code-block:: javascript
1214

13-
db.collection.find()._addSpecial("$showDiskLoc" , true)
14-
15+
db.collection.find( { <QUERY> } )._addSpecial("$showDiskLoc" , true)
16+
db.collection.find( { $query: { <QUERY> }, $showDiskLoc: true } )

0 commit comments

Comments
 (0)